By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

XML node containing markup

P: n/a
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,

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 13 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 13 '06 #2

P: n/a
Martin Honnen wrote:
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?

--
Dylan Parry - http://webpageworkshop.co.uk

A Flower?
Jul 13 '06 #3

P: n/a
Dylan Parry wrote:
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[i];
if (1 == cNode.nodeType) {
txt += getTextContent(cNode);
}
if (3 == cNode.nodeType){
txt += cNode.data;
}
}
return txt;
}

--
Rob
Jul 13 '06 #4

P: n/a
if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b]]></node>
then node.nodeValue will include the html.

Jul 13 '06 #5

P: n/a
go*****@gmail.com wrote:
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!

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 13 '06 #6

P: n/a


Dylan Parry wrote:
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?
if (typeof XMLSerializer != 'undefined')

See <http://www.faqts.com/knowledge_base/view.phtml/aid/34646/fid/616>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 13 '06 #7

P: n/a

Dylan Parry wrote:
go*****@gmail.com wrote:
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

--
Rob

Jul 13 '06 #8

P: n/a
RobG wrote:
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 ;)

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 14 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.