Solved

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

Posted on 2007-12-04
5
417 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now