File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having issue getting the same level nodes.

 
jo sim
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jo sim
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the output that im looking for is
index 1
C
fan
Title C1linkC1

index 2
C
fan
Title C2
linkC2

index 3
C
pen
Title C3
linkC2
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your XPath expression returns a list of three <details> elements. Your subsequent code just prints the names of those elements (namely "detail") and the value of their first children, which are always just whitespace text nodes. You don't want that.

But you say you want to print information about the children of those <details> elements. So you're going to have to write code to examine the children and -- for the children which are elements -- print the element name and the contents of the child's first child.
 
amit punekar
Ranch Hand
Posts: 544
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you should be using node.getTextContent() to get the values you want.

Thanks,
amit
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic