Solved

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

Posted on 2007-12-04
5
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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