kentcdodds kentcdodds - 10 months ago 69
Java Question

Get XML only immediate children elements by name

My question is: How can I get elements directly under a specific parent element when there are other elements with the same name as a "grandchild" of the parent element.

I'm using the Java DOM library to parse XML Elements and I'm running into trouble. Here's some (a small portion) of the xml I'm using:

<group name="" zip="true">
<file location="C:\valid\directory\" />
<file location="C:\another\valid\file.doc" />
<file location="C:\valid\file\here.txt" />
<file location="C:\valid\file.txt" />
<file location="C:\valid\file.xml" />
<file location="C:\valid\file.doc" />

As you can see, there are two places you can place the
element. Either in groups or outside groups. I really want it structured this way because it's more user-friendly.

Now, whenever I call
it gives me all the
elements, including those under the
element. I handle each of these kinds of files differently, so this functionality is not desirable.

I've thought of two solutions:

  1. Get the parent element of the file element and deal with it accordingly (depending on whether it's

  2. Rename the second
    element to avoid confusion.

Neither of those solutions are as desirable as just leaving things the way they are and getting only the
elements which are direct children of

I'm open to IMPO comments and answers about the "best" way to do this, but I'm really interested in DOM solutions because that's what the rest of this project is using. Thanks.

Answer Source

Well, the DOM solution to this question is actually pretty simple, even if it's not too elegant When I iterate through the filesNodeList which is returned when I call notificationElement.getElementsByTagName("file"); I just check whether the parent node's name is "notification." If it isn't then I ignore it because that will be handled by the <group> element. Here's my code solution:

for (int j = 0; j < filesNodeList.getLength(); j++) {
  Element fileElement = (Element) filesNodeList.item(j);
  if (!fileElement.getParentNode().getNodeName().equals("notification")) {