...

View Full Version : Reading CDATA Information from an XML tag



Skyzyx
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:


xmlDoc.getElementsByTagName('content')[0].firstChild.data;

liorean
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.

Skyzyx
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)

jkd
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();
r.selectNode(xmlDoc.getElementsByTagName("content")[0]);
alert(r.toString());

liorean
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.)

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

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum