...

View Full Version : Problem with Node as text



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


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

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


document.getElementsByTagName('mydata')[0].firstChild.data

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?

Thanks.

Kravvitz
03-06-2006, 02: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.

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

bigi
03-06-2006, 09: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?

[edit]
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..

Kor
03-06-2006, 10: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)?

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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++){
kids[i].nodeType==3?txt+=kids[i].data:null;
}
alert(txt)
}
</script>
</head>
<body>
<span>blah blah<br>one two<br>or three</span>
</body>
</html>

bigi
03-06-2006, 04: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.

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

bigi
03-06-2006, 09: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?

Kor
03-07-2006, 01:34 PM
Your answer might be here (the last post)
http://www.codingforums.com/showthread.php?t=81397
as I said, cloneNode() method might help you

glenngv
03-08-2006, 02: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.

liorean
03-08-2006, 02: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.

glenngv
03-08-2006, 10: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum