读取其中包含 ]] 的 XML CDATA 部分
我正在使用 XMLHttpRequest
在 Javascript 中编写 RSS 阅读器。
对于某些 RSS 提要,我没有遇到任何问题,但在某些情况下,xmlDocument.firstChild
属性始终为 NULL
在尝试查看有效 XML 和无效 XML 之间的差异之后不起作用我发现以下是错误的原因。
<item>
<description>
<![CDATA[This is a description for a test [...]]]>
</description>
</item>
因为在此描述标记中,我有一个右括号,后跟 CDATA 的右括号导致了我的错误,所以我使用 LINQ 为相同的 XML 使用 C# 编写了代码,并且一切正常。
CDATA 右括号之前的右括号导致了这种奇怪的行为。作为测试,我尝试使用 C# 和 LINQ 读取相同的 XML,一切正常。
然后我尝试在右括号之间添加一个空格,如下所示
结果成功了!
我的 javascript 代码
function LoadRSS() {
http_request.onreadystatechange = function () { showContent(http_request); };
http_request.open("GET", "./feeds/test.xml", true);
http_request.send(false);
}
function showContent(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var parser = new DOMParser();
var xml_doc = parser.parseFromString(http_request.responseText, "text/xml");
alert(xml_doc.firstChild)
}
else {
xml_doc = null;
}
}
}
有人遇到过类似的事情吗?现在我真的不知道如何进行,欢迎大家提出意见和建议。
I'm coding a RSS reader in Javascript using XMLHttpRequest
.
For some RSS Feeds I had no problems but in some cases the xmlDocument.firstChild
attribute was always NULL
After trying to see the differences between the XML that worked and the ones that didn't worked I found that the following is the cause of the error.
<item>
<description>
<![CDATA[This is a description for a test [...]]]>
</description>
</item>
Because that in this description tag I have a closing bracket followed by the closing brackets of the CDATA is causing my error, I've made a code with C# using LINQ for the same XML and everything worked.
The closing bracket that is just before the closing brackets of CDATA is causing this strange behaviour. As a test I've tried to read the same XML using C# and LINQ, everything worked okay.
Then I tried to add a space between the closing brackets, like the following
<![CDATA[This is a description for a test [...] ]]>
And it worked!
my javascript code
function LoadRSS() {
http_request.onreadystatechange = function () { showContent(http_request); };
http_request.open("GET", "./feeds/test.xml", true);
http_request.send(false);
}
function showContent(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var parser = new DOMParser();
var xml_doc = parser.parseFromString(http_request.responseText, "text/xml");
alert(xml_doc.firstChild)
}
else {
xml_doc = null;
}
}
}
Does anyone have faced something similar? Now I really don't know how to proceed any comments and suggestions are welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您使用什么浏览器,似乎都会错误地解析 CDATA 部分 - 只有
]]>
标记该部分的结尾,任何其他方括号根本不会影响这一点。Whatever browser you're using seems to be parsing CDATA sections incorrectly -- only
]]>
marks the end of the section, any other square brackets should not affect this at all.至于“如何继续”...为什么不在 CDATA 块末尾之前始终包含一个空格?您无法控制生成的 XML 吗?如果是这样,你可以使用 JS 来:
As for "how to proceed"...why not just include a space before the end of the CDATA block always? Do you not have control over the generated XML? If so, you could use JS to: