|
I am trying to get the value of the userId attribute for each of the "users". What follows is the sample xml and java code. The selectNodeList call seems to work fine. However, the select selectSingleNode calls do not work. Can anyone help?
<?xml version="1.0" encoding="UTF-8"?> <List> <User userId="1"/> <User userId="2"/> </List>
import java.io.File; import java.io.FileInputStream;
import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.apache.xpath.XPathAPI;
public class TestXPath { public TestXPath() { }
private void test() { try { File file = new File("D:/Users.xml"); FileInputStream fis = new FileInputStream(file); javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); javax.xml.parsers.DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(fis); NodeList list = XPathAPI.selectNodeList( doc, "/List/User" ); for (int i = 0; i < list.getLength(); i++) { Node user = list.item(i); Node userId = XPathAPI.selectSingleNode(user, "./User/@userId"); System.out.println(userId.getNodeValue()); } } catch (Exception e) { System.out.println("Oh no! Your xpath _expression_ did not succeed."); e.printStackTrace(); } }
public static void main(String[] args) { TestXPath testXPath1 = new TestXPath();
testXPath1.test(); } } |
- Re: XPathAPI class question with Xalan 2.2 Koes, Derrick
- Re: XPathAPI class question with Xalan 2.2 Christian Geuer-Pollmann
