View Full Version : Reading CDATA Information from an XML tag

03-20-2004, 12:08 AM
How would I go about using the DOM to read (and return) the value of an XML tag whose contents are wrapped in a CDATA section?

Currently, I'm pulling the data this way from non-CDATA tags:


03-20-2004, 12:16 AM
Depends on the DOM implementation. Some DOM implementations are bound to have inserted the CDATA section as a TextNode. Some may have the CDATA node there, though. In either case, either the data or the nodeValue property should contain the textual contents of it.

03-20-2004, 12:28 AM
data and nodeValue aren't returning anything (I'm getting blanks). I know that the rest of my code is fine, because I can parse the exact same tag without the CDATA there.

Would something like innerText or whatever work? (I'm dealing exclusively with Gecko 1.4 or newer)

03-20-2004, 12:55 AM
If you're dealing with Gecko exclusively, then you could probably just use a Range:

var r = xmlDoc.createRange();

03-20-2004, 01:06 AM
Skyzyx: Have you checked in the DOM inspector how the JavaScript object looks? You know, there might be whitespace nodes in between, which breaks your use of firstChild. Also, it might be that you need to normalise the content first. (Should eliminate all problems you might have with in-between nodes, if that's what the problem is really about.)

03-21-2004, 06:40 PM
How would you normalize the content?

03-21-2004, 06:47 PM
[object Element].normalize();

03-22-2004, 11:07 PM
Y'know what? It was a whitespace issue. Thanks for pointing me in the right direction guys. :D