5

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.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
YasuDevil
  • 470
  • 5
  • 16

2 Answers2

5

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.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
casablanca
  • 69,683
  • 7
  • 133
  • 150
  • Well, replacing the brackets problably will fix my problem. I'll try to run the same code in another Browser to see what happens. – YasuDevil Jan 11 '11 at 17:45
  • 1
    First part is right, second wrong -- yes, you can include entities, but no they will NOT be parsed but included exactly as they are. So you can not use entities to quote anything. – StaxMan Jan 11 '11 at 17:51
  • 1
    @StaxMan: Sorry, I misinterpreted that section -- it's only for attribute values, not for generic CDATA sections. @YasuDevil: I guess my solution won't work after all. – casablanca Jan 11 '11 at 18:01
  • Section you refer is confusing because it uses CDATA in different meaning; as "character data" data type (same way as DTDs allow cdata as one of types), not as CDATA section. Please check out XML specification, section 2.7 (CDATA); no entity reference substitution is done. This is one of benefits of using CDATA sections without having to quote ampersands or less-thans. – StaxMan Jan 11 '11 at 18:01
  • @casablanca Such entities--even if allowed in CDATA, which they are not--would need to be declared for the document. (Though who knows, perhaps this broken "OBIGO" browser will also blindly translate all HTML entities in XML.) – Phrogz Jan 11 '11 at 18:01
  • Actually the HTML entities (not familiar with the term but I'm believing that entities refers to the Tags) are rendered correctly, on one of my tests I put the body of the on the innerHTML of a DIV and the images are loaded correctly, I supose that is the expected behaviour right? Well, I did ran the almost same code in Firefox, some changes were necessary though but everything worked OK. On Chrome I had some permissions issues, but I think that the Firefox scenario prooves that there is a bug on Obigo. – YasuDevil Jan 11 '11 at 18:09
1

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");
Phrogz
  • 296,393
  • 112
  • 651
  • 745