View Full Version : Reading CDATA Information from an XML tag

03-20-2004, 01: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, 01: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, 01: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, 01:55 AM
If you're dealing with Gecko exclusively, then you could probably just use a Range:

var r = xmlDoc.createRange();

03-20-2004, 02: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, 07:40 PM
How would you normalize the content?

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

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