0

I've got a large XML file I'm parsing.

My first selectNodes statement gets me a list of all elements I want to parse through.

For each element returned by the first selectNodes statement, I want to run another selectNodes.

The XML might look like this:

<A>
    <B></B>
    <C></C>
</A>
<A>
    <B></B>
    <C></C>
</A>

I am restrained because I must parse the inner nodes, ie each subset of "A", one at a time.

The end goal is to do something with each B,C combination, one at at time.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Rubber Duck
  • 2,997
  • 2
  • 20
  • 25

2 Answers2

1

In xPath you can use a double slash (//) to find all nodes of a certain name in the parent node. If you wish to return all nodes within a document, your expression can start with a "//".

To return all B nodes and all C nodes in separate lists, try:

List<?> bNodes = document.selectNodes("//B");
List<?> cNodes = document.selectNodes("//C");

for (Object b : bNodes)
  for (Object c : cNodes)
    //do something with each b/c combination

Alternatively, to specify the root node to search in, use:

List<?> bNodes = document.selectNodes("A//B");
List<?> cNodes = document.selectNodes("A//C");

Where document is your file read with a SAXReader.

17slim
  • 1,233
  • 1
  • 16
  • 21
0

You can provide any node you want as the context of an XPath expression. Let's say you have an XPath expression to select the B node:

XPathExpression expr = xpath.compile("./B");

Let's suppose you have the A node in the variable Node aNode. You can select the B element using simply

Object result = expr.evaluate(aNode, XPathConstants.NODE);

Where result will be of type Node.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40