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