Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2006, 01:55 AM   PM User | #1
bigi
New to the CF scene

 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
bigi is an unknown quantity at this point
Problem with Node as text

Let's say my XML file is setup this way.
Code:
<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:
Code:
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.
bigi is offline   Reply With Quote
Old 03-06-2006, 02:06 AM   PM User | #2
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
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.
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Kravvitz is offline   Reply With Quote
Old 03-06-2006, 08:39 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
you may use cloneNode(true) method as well...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-06-2006, 09:31 AM   PM User | #4
bigi
New to the CF scene

 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
bigi is an unknown quantity at this point
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:
Code:
blah blah undefinedone two
instead of this:
Code:
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
Code:
[object text][object element][object text]
So I think I need a way to somehow print out that element as a text..

Last edited by bigi; 03-06-2006 at 09:51 AM..
bigi is offline   Reply With Quote
Old 03-06-2006, 10:41 AM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-06-2006, 10:56 AM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Something like this?:
PHP Code:
<!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> 
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-06-2006, 04:17 PM   PM User | #7
bigi
New to the CF scene

 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
bigi is an unknown quantity at this point
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.
bigi is offline   Reply With Quote
Old 03-06-2006, 04:35 PM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
What do you want to do with all that "inner content" of that element? That is the question.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-06-2006, 09:22 PM   PM User | #9
bigi
New to the CF scene

 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
bigi is an unknown quantity at this point
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?
bigi is offline   Reply With Quote
Old 03-07-2006, 01:34 PM   PM User | #10
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Your answer might be here (the last post)
http://www.codingforums.com/showthread.php?t=81397
as I said, cloneNode() method might help you
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-08-2006, 02:07 AM   PM User | #11
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
The XML should be like this.
Code:
<mydata><![CDATA[blah blah<br />one two]]></mydata>
Everything inside a CDATA section is ignored by the parser.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 03-08-2006, 02:55 AM   PM User | #12
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by glenngv
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.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 03-08-2006, 10:44 AM   PM User | #13
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I just copied that from w3schools. 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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:08 AM.


Advertisement
Log in to turn off these ads.