Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Howto select a node from a result node using XPathAPI?

843834Feb 4 2008 — edited Feb 5 2008
hi
after hours of struggling, i have to bring it here.
i m using XPath to retrieve elements from an XML document, and trying to get elements from the retrieved elements, it basically looks like this:
////////// XML ///////////
<?xml version='1.0'?>
<root id='1'>
   <elem code='first' id='c1'>
      <value>first node</value>
   </elem>
   <elem code='second' id='c2'>
      <value>second node</value>
   </elem>
   <container code='CONTAINER' id='rows'>
      <container code='row1' id='r1'>
         <elem code='inside1' id='item1'>
            <value>inside node 1</value>
         </elem>
         <elem code='inside2' id='item2'>
            <value>inside node 2</value>
         </elem>
      </container>
      <container code='row2' id='r2'>
            ...
      </container>
   </container>
</root>

/***** JAVA *******/
NodeList containers = XPathAPI.selectNodeList(xmlDoc, "//container[@code != 'CONTAINER']");
for (int i = 0; i < containers.getLength(); i ++) {
   Node container = containers.item(i);
   NodeList elems = XPathAPI.selectNodeList(container, "//elem"); // i suppose this will get the 'elem' nodes from one container
   for (int e = 0; e < elems.getLength(); e++) {
      Node elem = elems.item(e);
      Node code = XPathAPI.selectSingleNode(elem, "//@code");
      Node value = XPathAPI.selectSingleNode(elem, "//value");

      System.out.println(code.getFirstChild().getNodeValue().trim() + " = " + value.getFirstChild().getNodeValue().trim());
   }
}
the problem is that the XPathAPI.getSingleNode doesn't return the correct values, even i use the node 'container' as the starting point for the lookup, it returns all 'elem' nodes of the XML document, it doesn't seem to work.

can anyone help me? thanks alot!

best regards

Edited by: siab on Feb 4, 2008 4:18 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2008
Added on Feb 4 2008
2 comments
425 views