View Full Version : Problem with Node as text

03-06-2006, 02:55 AM
Let's say my XML file is setup this way.

<mydata>blah blah<br />one two</mydata>

I am trying to use the Node.firstChild.data to print out the text in the < mydata > node.
For example:


The problem is, this returns only "blah blah". I think this is because it treats the < br / > tag as a separate child node, so when I do firstChild it only gets "blah blah". I want it to treat the whole text inside the < mydata > node as one child node and print it out.

How can I do this?


03-06-2006, 03:06 AM
Yes, the <br> element is the second child node.

Use a for loop to loop through the child nodes and concatenate all of the text nodes.

03-06-2006, 09:39 AM
you may use cloneNode(true) method as well...

03-06-2006, 10:31 AM
I tried to use a loop to loop through the child nodes.
The problem is, the <br /> tag somehow worked as a delimiter between nodes and returned as " undefined"....
So in my output, I get:

blah blah undefinedone two
instead of this:

blah blah
one two
Which is my desired result.

What is cloneNode?

just tried something else. I did .nodeValue instead of .data and got " null" in place of the " undefined".
if I just try to print the objects instead of data or nodeValue I get

[object text][object element][object text]

So I think I need a way to somehow print out that element as a text..

03-06-2006, 11:41 AM
Let us clarify... What do you mean by "print out". You need to return a string with the text (without br, p or whichever)?

03-06-2006, 11:56 AM
Something like this?:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload = function(){
var kids = document.getElementsByTagName('span')[0].childNodes;
var txt ='';
for(var i=0;i<kids.length;i++){
<span>blah blah<br>one two<br>or three</span>

03-06-2006, 05:17 PM
I actually wanted the whole thing inside <mydata> be treated as text string, *with* the br, p, and whatever html tags in there.
I am reading an XML file that has no "real" HTML tags in there, only my own XML tags with names like <name>, <date>, etc.

03-06-2006, 05:35 PM
What do you want to do with all that "inner content" of that element? That is the question.

03-06-2006, 10:22 PM
well, right now I store it in a string and then use .innerHTML to replace a text somewhere on the page with this new text.
That is the same as printing it out, I assume?

03-07-2006, 02:34 PM
Your answer might be here (the last post)
as I said, cloneNode() method might help you

03-08-2006, 03:07 AM
The XML should be like this.

<mydata><![CDATA[blah blah<br />one two]]></mydata>
Everything inside a CDATA section is ignored by the parser.

03-08-2006, 03:55 AM
Everything inside a CDATA section is ignored by the parser.Correction: It's treated as raw text. It isn't ignored, it's just not parsed for any markup or entity references. The contents still render.

Comments on the other hand are ignored. The contents are not rendered.

03-08-2006, 11:44 AM
I just copied that from w3schools (http://www.w3schools.com/xml/xml_cdata.asp). What probably w3schools' context (and I agree with it) when it said "ignored by the parser" is the text is ignored in terms of parsing and not rendering. You've taken "ignored" out of context.