Reading the XML CDATA section with]] in it

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.

+5
source share
2 answers

No matter which browser you use, it seems to handle CDATA partitions incorrectly - only ]]>marks the end of the section, any other square brackets should not affect this at all.

+5
source

As for how to act ... why not just include a space before the end of the CDATA block? You have no control over the generated XML? If so, you can use JS to:

var xml = http_request.responseText.replace( /\]\]>/g, ' ]]>' );
var xml_doc = parser.parseFromString(xml, "text/xml");
+1
source

All Articles