?
Solved

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

Posted on 2007-12-04
5
Medium Priority
?
455 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 2000 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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