childNodes.length problem

Discussion in 'Javascript' started by Q1tum, Jan 4, 2006.

  1. Q1tum

    Q1tum Guest

    Hi all,

    I have a problem with getting the amount of childs in a XML structure,
    the strucure is somewhat like the following:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <cms>
    <num>21</num>
    <xmlnames>
    <field>id</field>
    <field>name</field>
    <field>availablefrom</field>
    <field>availableuntill</field>
    <field>owner</field>
    </xmlnames>
    </cms>

    If I use the following to get te amount of childs for xmlnames I get an
    amount of 11:
    xmlObj.responseXML.getElementsByTagName('xmlnames')[0].childNodes.length

    Is this wrong or maybe there is a better / other way of counting the
    child amount?

    Regards,

    Arno
     
    Q1tum, Jan 4, 2006
    #1
    1. Advertising

  2. Q1tum

    Lee Guest

    Q1tum said:
    >
    >Hi all,
    >
    >I have a problem with getting the amount of childs in a XML structure,
    >the strucure is somewhat like the following:
    >
    ><?xml version="1.0" encoding="iso-8859-1"?>
    ><cms>
    > <num>21</num>
    > <xmlnames>
    > <field>id</field>
    > <field>name</field>
    > <field>availablefrom</field>
    > <field>availableuntill</field>
    > <field>owner</field>
    > </xmlnames>
    ></cms>
    >
    >If I use the following to get te amount of childs for xmlnames I get an
    >amount of 11:
    >xmlObj.responseXML.getElementsByTagName('xmlnames')[0].childNodes.length
    >
    >Is this wrong or maybe there is a better / other way of counting the
    >child amount?


    I count 11 child nodes in your examples.
    Five of them are element nodes.
    The other six are text nodes containing whitespace.
     
    Lee, Jan 4, 2006
    #2
    1. Advertising

  3. Q1tum

    RobG Guest

    Q1tum wrote:
    > Hi all,
    >
    > I have a problem with getting the amount of childs in a XML structure,
    > the strucure is somewhat like the following:
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <cms>
    > <num>21</num>
    > <xmlnames>
    > <field>id</field>
    > <field>name</field>
    > <field>availablefrom</field>
    > <field>availableuntill</field>
    > <field>owner</field>
    > </xmlnames>
    > </cms>
    >
    > If I use the following to get te amount of childs for xmlnames I get an
    > amount of 11:
    > xmlObj.responseXML.getElementsByTagName('xmlnames')[0].childNodes.length


    Which seems correct to me. Empty text nodes will be inserted at the
    end of each line where there is a new line or line feed character, so
    there are empty text nodes after the opening 'xmlnames' tag and the 5
    closing 'field' tags that are all children of the xmlnames element.

    That's 6 empty text nodes plus the 5 'field' child nodes, making 11.


    >
    > Is this wrong


    No.


    > or maybe there is a better / other way of counting the
    > child amount?


    If you want to get just the children with a tagName of 'field', then
    use getElementsByTagName(). Note that will get all the descendants of
    xmlnames that have a tag called 'field', not just the direct children.

    If you want explicitly the children, then use a loop to get them:

    var t = xmlObj.responseXML.getElementsByTagName('xmlnames');
    var c = t[0].childNodes;
    var k = [];
    for (var i=0, len=c.length; i<len; ++i){
    if('field' == c.tagName){
    k[k.length] = c;
    }
    }

    // k is now an array of the child nodes of xmlnames that are
    // field nodes.



    --
    Rob
     
    RobG, Jan 4, 2006
    #3
  4. Q1tum wrote:


    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <cms>
    > <num>21</num>
    > <xmlnames>
    > <field>id</field>
    > <field>name</field>
    > <field>availablefrom</field>
    > <field>availableuntill</field>
    > <field>owner</field>
    > </xmlnames>
    > </cms>
    >
    > If I use the following to get te amount of childs for xmlnames I get an
    > amount of 11:
    > xmlObj.responseXML.getElementsByTagName('xmlnames')[0].childNodes.length
    >
    > Is this wrong or maybe there is a better / other way of counting the
    > child amount?


    childNodes contains all kind of nodes and in your case there a text
    nodes with white space between the element nodes e.g. you have

    <xmlnames>^^^^^white space text node here
    ^^^^^ <field>id</field>^^^^ and here
    ^^^^^ <field>name</field>^^^^ and here ^^^^
    </xmlnames>

    and not

    <xmlnames><field>id</field><field>name</field></xmlnames>

    If you are looking only for element nodes then depending on what you
    need and depending on the structure of the XML you need to loop through
    childNodes and check for nodeType == 1 or you can use
    getElementsByTagName('*') or in your case simply

    xmlObj.responseXML.getElementsByTagName('xmlnames')[0].getElementsByTagName('field')

    Be aware that getElementsByTagName finds all descendant elements so with
    nested structures it is not equivalent to childNodes and checking
    nodeType == 1.

    But in many cases including your example file when people run into white
    space text nodes issues with childNodes then simply switching to
    getElementsByTagName to find the elements needed works without problems.

    Besides that there is also XPath and NodeFilter/TreeWalker APIs but
    support for that is not as wide spread as childNodes/nodeType and
    getElementsByTagName and XPath for instance is supported in MSXML 3 and
    later for IE/Win and in Mozilla but the APIs are very different.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 4, 2006
    #4
  5. Q1tum

    Q1tum Guest

    Thank you,

    your script works fine!

    Martin Honnen thanks as well for explaining
     
    Q1tum, Jan 4, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jake Barnes
    Replies:
    2
    Views:
    130
    GĂ©rard Talbot
    Feb 7, 2006
  2. Q1tum
    Replies:
    12
    Views:
    553
  3. Moses

    Mozilla Vs IE for childNodes.length

    Moses, Oct 20, 2006, in forum: Javascript
    Replies:
    5
    Views:
    153
    Moses
    Oct 20, 2006
  4. lmg

    xml node.childNodes.length

    lmg, Apr 1, 2008, in forum: Javascript
    Replies:
    1
    Views:
    203
  5. okey
    Replies:
    2
    Views:
    168
    David Mark
    May 24, 2009
Loading...

Share This Page