Return to Snippet

Revision: 51484
at September 26, 2011 19:23 by studioevoque


Initial Code
public static void examineXmlFile(String path) throws Exception {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(path);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//node/@id");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            System.out.println(nodes.item(i).getNodeValue());
        }
    }

Initial URL
http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

Initial Description


Initial Title
Simple XPath expression evaluation

Initial Tags


Initial Language
Java