I am coding an RSS reader in Javascript using XMLHttpRequest.
For some RSS feeds I had no problems, but in some cases the attribute was xmlDocument.firstChildalwaysNULL
After trying to see the differences between processed XML and those that didn’t work, I found that the cause was an error.
<item>
<description>
<![CDATA[This is a description for a test [...]]]>
</description>
</item>
Because in this description tag, I have a closing bracket followed by CDATA closing brackets, it causes my error, I made C # code using LINQ for the same XML, and it worked.
The closing parenthesis immediately preceding the closing CDATA brackets causes this strange behavior. As a test, I tried to read the same XML using C # and LINQ, everything worked fine.
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;
}
}
}
Has anyone come across something similar? Now I really don't know how to get down to the comments and suggestions.
source
share