XML node containing markup

D

Dylan Parry

Hi folks,

I have an XML node called "myNode" and it contains:

"This is some text"

Now I can use the myNode.nodeValue property to get the string of text
above. But say myNode contains:

"This is <em>some text</em>"

The property myNode.nodeValue will now be "This is ". How can I get the
exact string above returned?

Thanks,
 
M

Martin Honnen

Dylan Parry wrote:

I have an XML node

What kind of node is that exactly, an element node, a text node, a
fragment node?

"This is <em>some text</em>"

How can I get the
exact string above returned?

It depends on the XML object model you use, with MSXML that you use in
ASP or in IE you can serialize each node using its property named 'xml' so
node.xml
gives you the serialized XML of the node.
However in the above case you have
This is <em>some text</em>
contained in some other node (probably an element node) and you need to
be aware that if you use
node.xml
on the container node that you get the start tag, attributes, and end
tag of the container node serialized as well.
Thus if you only want to serialize the child nodes then you need to loop
through and concatenate the xml of each child node (a text node, an
element node, a text node in that example).

With the W3C DOM to serialize a DOM node you would need DOM Level 3 Load
and Save, currently inside browsers only Opera 8 and 9 have a minimal
implementation of that.
However Mozilla introduced the proprietary XMLSerializer and other
browsers have implemented that too (Opera, recent Safari, Konqueror) so
there to serialize a node you do e.g.
var xmlSerializer = new XMLSerializer();
then to serialize a node to a string
var s = xmlSerializer.serializeToString(node);
Again for your case above you would need to loop over the child nodes of
the container node and serialize each child node.
 
D

Dylan Parry

Martin said:
node.xml [or]
var s = xmlSerializer.serializeToString(node);

Thanks. The above two, or rather a combination of the above, will do the
job nicely. I've set up a for loop to concatenate all of the nodes, but
obviously IE supports the former and not the latter, and everything else
seems to support the latter and not the former.

This is where I need to test for supported features! The trouble is that
I can't seem to figure out how to test for support of "node.xml" nor
"xmlSerializer" :( Any pointers?
 
R

RobG

Dylan said:
Hi folks,

I have an XML node called "myNode" and it contains:

"This is some text"

Now I can use the myNode.nodeValue property to get the string of text
above. But say myNode contains:

"This is <em>some text</em>"

The property myNode.nodeValue will now be "This is ". How can I get the
exact string above returned?

You can use textContent, and if not supported recurse down all the
childNodes collecting the text content of all the text nodes, something
like:

function getTextContent(el)
{
// Use textContent if supported
if (typeof el.textConent == 'string') return el.textContent;

// Otherwise, recurse down the childNodes & siblings
var cNode, cNodes = el.childNodes;
var txt = '';
for (var i=0, len=cNodes.length; i<len; ++i){
cNode = cNodes;
if (1 == cNode.nodeType) {
txt += getTextContent(cNode);
}
if (3 == cNode.nodeType){
txt += cNode.data;
}
}
return txt;
}
 
G

goulart

if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b> ]]></node>


then node.nodeValue will include the html.
 
D

Dylan Parry

if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b> ]]></node>

then node.nodeValue will include the html.

Hmm, that looks like a much better solution all round, to be honest!
 
R

RobG

Dylan said:
if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b> ]]></node>

then node.nodeValue will include the html.

Hmm, that looks like a much better solution all round, to be honest!

Ah, so you wanted to *keep* the markup... :-x
 
D

Dylan Parry

RobG said:
Ah, so you wanted to *keep* the markup... :-x

Yes :) It's a bit of a cludge, but I found that suddenly I needed an XML
node to contain an anchor element that I really didn't want to have to
deal with when I parse the file. I suppose it's just me being lazy for
the time being, but with working to a tight deadline I don't have time
(yet) to rewrite too much code. Saying that, I do have "REWRITE ENTIRE
JS LIBRARY" as a big heading, so it *will* happen at some point ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top