get the number of children of a given element

Discussion in 'Java' started by Matt, Jun 4, 2004.

  1. Matt

    Matt Guest

    I want to get the number of children of a given element. Below is my
    code attempt and
    the xml sample file. The caller is getElementChildNumber("persons");,
    I expect
    to get 2. Since there are 2 children that are under persons element.
    Any ideas??

    /**
    get the number of children of a given element
    */
    public int getElementChildNumber(String elementName)
    {
    DocumentBuilderFactory dbf = null;
    DocumentBuilder db = null;
    Document doc = null;
    try
    {
    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();
    doc = db.parse(fileName);
    NodeList nodelist = doc.getElementsByTagName(elementName);
    return nodelist.getLength();
    }
    catch (Exception e)
    { e.printStackTrace();
    }
    return -1;
    }

    <?xml version="1.0" encoding="UTF-8"?>
    <response>
    <persons>
    <person>
    <name>Joe</name>
    <age>10</age>
    </person>
    <person>
    <name>Mark</name>
    <age>30</age>
    </person>
    </persons>
    </response>
    Matt, Jun 4, 2004
    #1
    1. Advertising

  2. Matt

    Andy Fish Guest

    reading the manual, it says that getElementsByTagName will return a list of
    all the elements of that tag name. since you have one 'persons' element, I
    would expect your function to return 1.

    if you want to count the number of children under a given element, call
    getChildNodes() on it.

    note that this will probably also return some whitespace nodes, so you might
    have to filter through the list if you only want to count the elements


    "Matt" <> wrote in message
    news:...
    > I want to get the number of children of a given element. Below is my
    > code attempt and
    > the xml sample file. The caller is getElementChildNumber("persons");,
    > I expect
    > to get 2. Since there are 2 children that are under persons element.
    > Any ideas??
    >
    > /**
    > get the number of children of a given element
    > */
    > public int getElementChildNumber(String elementName)
    > {
    > DocumentBuilderFactory dbf = null;
    > DocumentBuilder db = null;
    > Document doc = null;
    > try
    > {
    > dbf = DocumentBuilderFactory.newInstance();
    > db = dbf.newDocumentBuilder();
    > doc = db.parse(fileName);
    > NodeList nodelist = doc.getElementsByTagName(elementName);
    > return nodelist.getLength();
    > }
    > catch (Exception e)
    > { e.printStackTrace();
    > }
    > return -1;
    > }
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <response>
    > <persons>
    > <person>
    > <name>Joe</name>
    > <age>10</age>
    > </person>
    > <person>
    > <name>Mark</name>
    > <age>30</age>
    > </person>
    > </persons>
    > </response>
    Andy Fish, Jun 4, 2004
    #2
    1. Advertising

  3. Matt

    Guest

    Peace.
    Here is an xpath solution for the casual onlooker.
    Tested on Mozilla.org Firefox and Microsoft Internet Explorer
    returns
    Node Count for Person:4
    Node Count for Name:4
    Node Count for Age:2


    count.xml count.xsl

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="count.xsl"?>
    <response>
    <persons>
    <person>
    <name>Joe</name>
    <age>10</age>
    </person>
    <person>
    <name>Mark</name>
    <age>30</age>
    </person>
    <person>
    <name>Khan</name>
    </person>
    <person>
    <name>Omar</name>
    </person>
    </persons>
    </response>


    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="person" select="count(/response/persons/person)" />
    <xsl:variable name="name" select="count(/descendant::name)" />
    <xsl:variable name="age" select="count(/descendant::age)" />
    <xsl:template match="/">
    <html>
    <body>
    Node Count for Person:<xsl:copy-of select="$person" /> <br />
    Node Count for Name:<xsl:copy-of select="$name" /> <br />
    Node Count for Age:<xsl:copy-of select="$age" /> <br />
    <br />
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    Have a good day.

    -- References
    <a_6wc.190$>
    , Jun 5, 2004
    #3
    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. Matt
    Replies:
    1
    Views:
    382
    Martin Honnen
    Jun 5, 2004
  2. Matt
    Replies:
    2
    Views:
    833
    Ben Edgington
    Oct 12, 2004
  3. Kourosh
    Replies:
    1
    Views:
    110
    Kourosh
    Jun 8, 2006
  4. \A_Michigan_User\
    Replies:
    4
    Views:
    140
    Thomas 'PointedEars' Lahn
    Nov 16, 2007
  5. okey
    Replies:
    2
    Views:
    158
    David Mark
    May 24, 2009
Loading...

Share This Page