This is my XML
<?xml version="1.0"?>
<root>
<details>
<list>A</list>
<categ/>
<title>Title A1</title>
<link>linkA1</link>
</details>
<details>
<list>A</list>
<categ/>
<title>Title A2</title>
<link>linkA2</link>
</details>
<details>
<list>B</list>
<categ/>
<title>TitleB1</title>
<link>linkB1</link>
</details>
<details>
<list>B</list>
<categ/>
<title>Title B2</title>
<link>linkB2</link>
</details>
<details>
<list>C</list>
<categ>fan</categ>
<title>Title C1</title>
<link>linkC1</link>
</details>
<details>
<list>C</list>
<categ>fan</categ>
<title>Title C2</title>
<link>linkC2</link>
</details>
</details>
<details>
<list>C</list>
<categ>pen</categ>
<title>Title C3</title>
<link>linkC2</link>
</details>
</root>
based on this XML I need to get all the nodes under <details> where <list> is C so for exmaple if the list is C than I need all the nodes and their values where list = C I can do that by providing this in my xpath.
Document
doc = db.parse(file.xml);
doc.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList resultNode = (NodeList)xpath.evaluate("//details[list = 'C']", doc, XPathConstants.NODESET);
for (int i=0; i < resultNode.getLength(); i++){
Node n = resultNode.item(i);
System.out.println("Node Name: " + n.getNodeName());
System.out.println("Node Value: " + n.getChildNodes().item(0).getNodeValue());
}
// resultNode.getLength() gives me 3 which is correct. I need to be able to get the nodename under details for each where list = C
The result for the above Xpath in the statemtnt should be
<list>C</list>
<categ>fan</categ>
<title>Title C1</title>
<link>linkC1</link>
</details>
<details>
<list>C</list>
fan</categ>
Title C2
linkC2
Details for 3
C
pen
Title C3
linkC2