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 ments and suggestions are weled.
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 ments and suggestions are weled.
Share Improve this question edited Jan 11, 2011 at 17:38 Phrogz 304k113 gold badges667 silver badges757 bronze badges asked Jan 11, 2011 at 17:29 YasuDevilYasuDevil 4805 silver badges16 bronze badges 3- 1 This appears to be a bug in the XML parser. What OS/browser/version are you using? – Phrogz Commented Jan 11, 2011 at 17:37
- The browser is called OBIGO, it's a browser for cellphones – YasuDevil Commented Jan 11, 2011 at 17:40
- 1 Yep, broken browser. CDATA section can contain ']]' binations; only full ']]>' ends it. – StaxMan Commented Jan 11, 2011 at 17:52
2 Answers
Reset to default 5Whatever 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.
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:
var xml = http_request.responseText.replace( /\]\]>/g, ' ]]>' );
var xml_doc = parser.parseFromString(xml, "text/xml");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744288469a4566916.html
评论列表(0条)