aspose file tools*
The moose likes XML and Related Technologies and the fly likes having issue getting the same level nodes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "having issue getting the same level nodes." Watch "having issue getting the same level nodes." New topic
Author

having issue getting the same level nodes.

jo sim
Ranch Hand

Joined: Apr 24, 2006
Posts: 37
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

Joined: Apr 24, 2006
Posts: 37
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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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

Joined: May 14, 2004
Posts: 507
Hi,
you should be using node.getTextContent() to get the values you want.

Thanks,
amit


Regards,
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: having issue getting the same level nodes.
 
Similar Threads
Writing XML element
Fresher ! need help
XPath expression constrained by a predicate doesn't work (//element[@attribute="value"])
Why is this happening?
help with web service to display xml file contents using netbeans