Parse Nested XML tags with the same name

Posted by footose on Stack Overflow See other posts from Stack Overflow or by footose
Published on 2010-03-21T15:59:47Z Indexed on 2010/03/21 16:11 UTC
Read the original article Hit count: 320

Filed under:
|
|
|

Let's take a simple XML document:

  <x>
    <e>
        <e>
            <e>Whatever 1</e>
        </e>
    </e>
    <e>
        <e>
            <e>Whatever 2</e>
        </e>
    </e>
    <e>
        <e>
            <e>Whatever 3</e>
        </e>
    </e>
  </x> 

Using the standard org.w3c.dom, I can get the nodes in X by doing..

NodeList fullnodelist = doc.getElementsByTagName("x");

But if I want to return the next set of "e" I try to use something like ..

Element element = (Element) fullnodelist.item(0);
NodeList nodes = pelement.getElementsByTagName("e");

Expecting it to return "3" nodes (because there are 3 sets of "e"), but instead, it returns "9" - becuase it gets all entries with "e" apperently.

This would be fine in the above case, because I could probably iterate through and find what I'm looking for. The problem I'm having is that when the XML file looks like the following:

  <x>
    <e>
      <pattern>whatever</pattern>
      <blanks>
        <e>Something Else</e>
      </blanks>
    </e>
    <e>
      <pattern>whatever</pattern>
      <blanks>
        <e>Something Else</e>
      </blanks>
    </e>
  </x>

When I request the "e" value, it returns 4, instead of (what i expect) 2.

Am I just not understanding how the DOM parsing works? Typically in the past I have used my own XML documents so I would never name the items like this, but unfortunately this is not my XML file and I have no choice to work like this.

What I thought I would do is write a loop that "drills down" nodes so that I could group each node together...

public static NodeList getNodeList(Element pelement, String find)
    {
        String[] nodesfind = Utilities.Split(find, "/");
        NodeList nodeList = null;

        for (int i = 0 ; i <= nodesfind.length - 1; i++ )
        {
            nodeList = pelement.getElementsByTagName( nodesfind[i] );
            pelement = (Element)nodeList.item(i);
        }

        // value of the nod we are looking for
        return nodeList;
    }

.. So that if you passed "s/e" into the function, it would return the 2 nodes that I'm looking for (or elements, maybe I'm using the terminology incorrect?). instead it returns all of the "e" nodes within that node.

I'm using J2SE for this, so options are rather limited. I can't use any third party XML Parsers.

Anyway, if anyone is still with me and has a suggestion, it would be appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml