Solved

XML & Java / org.w3c.dom; How to get only direct child nodes. (no grandchildern)

Posted on 2007-12-04
5
426 Views
Last Modified: 2008-02-01
Given the attached XML.

Suppose I'm at the outer most node. I want to get a NodeList of the childern of my current node named `A` but only direct children.

This should behave similar to the getElementsByTagName("A") method.


<A>
  <A>
  </A>
  <A>
  <A>
  </B>
  </B>
  <A>
    <A> <!-- I Don't want this one. -->
    </A>
  </A>
</A>

Open in new window

0
Comment
Question by:modsiw
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 20405240
Probably easiest to use XPath. That would be pattern

./A
0
 
LVL 3

Author Comment

by:modsiw
ID: 20405407
Looks good. =)

Below seems to be the common usage of XPath. Please confirm?

It looks like it always uses the Document itself as the starting point of the query. Is there anyway to get it to use another node as the start point for the query?


String strQuery = "something";
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(strQuery);
NodeList nList = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

Open in new window

0
 
LVL 3

Author Comment

by:modsiw
ID: 20405456
Never mind my last. Stupid question. I read examples / tutorials and not docs.

Thanks much CEHJ. I believe this will work nicely.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20405577
:-)
0
 
LVL 3

Author Comment

by:modsiw
ID: 20405618
Can I use multiple XPath`s to make multiple XPathExpression`s?

If so, (And I presume I can), what would be the reason for making multiple XPath`s from a single factory?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question