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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing XML nonsense

    I have an xml like this..

    <system>
    <lot1>data1</lot1>
    <lot2>data2</lot2>
    <lot3>data3</lot3>
    .
    .
    .
    <lotn>datan</lotn></system>

    And I am having the hardest time retrieving the data using DOM. The n is undetermined and can take on any value greater than or equal to 1.

    var x=xmlDoc.getElementsByTagName("system");
    document.writeln( x[0].childNodes[0].nodeValue );

    that line should write data1 to the screen, but instead writes a big blank whitespace.

  • #2
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    The first child of the system element in that example is a text node, containing a line break. As such, look at
    Code:
    document.writeln( x[0].childNodes[1].nodeValue );

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    the following syntax is a little more robust (it ignore text nodes):

    Code:
    x[0].getElementsByTagName("lot1")
    x[0].getElementsByTagName("lot2")
    x[0].getElementsByTagName("lot3")
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought about getting tag element by name.. but the number of lots is undetermined. When would you stop? How could you know the number of them. x[0].length returns 1.

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Play with this:
    Code:
    var kid = myNode.parentNode.nextSibling;
        
    //Make sure we do not get a whitespace node
    while(kid != null && kid.nodeType != 1){
        kid = kid.nextSibling;
    }
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #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
    Your XML is not quite well formatted. What about?:
    Code:
    <system>
    <lot>data1</lot>
    <lot>data2</lot>
    <lot>data3</lot>
    .
    .
    .
    <lot>datan</lot>
    </system>
    There is no reason for you should use <lotn> tags within an XML file. Anyway, you should not need that indentation, as long as the text value datan should be, in fact, what you are interested in.

    But even if you might need that, you have 2 possibilities:

    1. using a while loop
    2. using attributes
    <lot val="1">data1</lot>
    <lot val="2">data2</lot>
    Last edited by Kor; 12-17-2008 at 02:39 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

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