Query on text and element nodes

Discussion in 'XML' started by arvindsd@yahoo.com, May 23, 2005.

  1. Guest

    Hi all,

    The code below gets me a list of all the nodes within the node object
    called xml_tags_root.

    NodeList nl = xml_tags_root.getChildNodes();

    The length of nodelist I get is double the number of actual elements!
    This is because after every element node there is text node which is (I
    think) the whitespace in the XML document.

    Is there a way to get only the element nodes?

    Please advice.

    thanks,
    ASD
    , May 23, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi all,
    >
    > The code below gets me a list of all the nodes within the node object
    > called xml_tags_root.
    >
    > NodeList nl = xml_tags_root.getChildNodes();
    >
    > The length of nodelist I get is double the number of actual elements!
    > This is because after every element node there is text node which is (I
    > think) the whitespace in the XML document.
    >
    > Is there a way to get only the element nodes?


    Well just loop through them and discard the text-nodes.

    > Please advice.
    >
    > thanks,
    > ASD
    >
    Tjerk Wolterink, May 23, 2005
    #2
    1. Advertising

  3. wrote:

    > The code below gets me a list of all the nodes within the node object
    > called xml_tags_root.
    >
    > NodeList nl = xml_tags_root.getChildNodes();
    >
    > The length of nodelist I get is double the number of actual elements!
    > This is because after every element node there is text node which is (I
    > think) the whitespace in the XML document.
    >
    > Is there a way to get only the element nodes?


    Depending on the parser and the API you are using there might be ways to
    only access the elements nodes, either by having white space text nodes
    ignored during parsing and not included in the DOM tree at all or by
    using a selection method later, for instance XPath, to access only
    element nodes.
    But as you have not provided any details on which parser/API you are
    using it is hard to tell. I guess you are using Java and the W3C DOM, in
    Java 1.5 you could use XPath.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 23, 2005
    #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. asd
    Replies:
    3
    Views:
    418
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    493
    gavnosis
    Aug 2, 2003
  3. Xamle Eng

    Why treat text nodes as nodes?

    Xamle Eng, May 13, 2005, in forum: XML
    Replies:
    8
    Views:
    476
    Fredrik Lundh
    May 28, 2005
  4. Replies:
    5
    Views:
    464
    bouton
    Sep 22, 2005
  5. HANM
    Replies:
    2
    Views:
    684
    Joseph Kesselman
    Jan 29, 2008
Loading...

Share This Page