Get XML only immediate children elements by name

mojomojo

New Member
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:\[code\]<notifications> <notification> <groups> <group name="zip-group.zip" zip="true"> <file location="C:\valid\directory\" /> <file location="C:\another\valid\file.doc" /> <file location="C:\valid\file\here.txt" /> </group> </groups> <file location="C:\valid\file.txt" /> <file location="C:\valid\file.xml" /> <file location="C:\valid\file.doc" /> </notification></notifications>\[/code\]As you can see, there are two places you can place the \[code\]<file>\[/code\] element. Either in groups or outside groups. I really want it structured this way because it's more user-friendly.Now, whenever I call \[code\]notificationElement.getElementsByTagName("file");\[/code\] it gives me all the \[code\]<file>\[/code\] elements, including those under the \[code\]<group>\[/code\] element. I handle each of these kinds of files differently, so this functionality is not desirable.I've thought of two solutions:[*]Get the parent element of the file element and deal with it accordingly (depending on whether it's \[code\]<notification>\[/code\] or \[code\]<group>\[/code\].[*]Rename the second \[code\]<file>\[/code\] element to avoid confusion.Neither of those solutions are as desirable as just leaving things the way they are and getting only the \[code\]<file>\[/code\] elements which are direct children of \[code\]<notification>\[/code\] elements.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.
 
Back
Top