Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    you may use cloneNode(true) method as well...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    What do you want to do with all that "inner content" of that element? That is the question.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Your answer might be here (the last post)
    [SOLVED] is <br> a childNode?
    as I said, cloneNode() method might help you
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #12
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •