• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

having issue getting the same level nodes.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
you should be using node.getTextContent() to get the values you want.

Thanks,
amit
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic