tags:

views:

195

answers:

2

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.

+1  A: 

You need to learn about XPath. There's an XPathFactory in stock Java 1.5, and you can create an XPath to name the particular 'e''s that you want.

bmargulies
J2SE on BlackBerry does not support XPath unfortunately.
footose
Well, then, you didn't mention that. Manual traversal is what you need to do.
bmargulies
That's kind of what I'm trying to do, but again, the above problem seems to arise. :(
footose
+1  A: 

If you are manual traversing the Xml, try using a variable which increments as you encounter each "e" tag, then decrements as you leave it.

If the source follows the above example you gave, you could use a simple if statement to make sure that the counter is equal to 2 before performing an action (assuming it started at 0)

I might have slightly misunderstood your exact problem, but I hope this helps.

davidb
Thanks david, I actually did this exactly. I had to basically "count" the amount of "e"s and then use each node plus the amount from the last time to 'drill' into the next one.. ..kind of confusing at first..
footose