Example usage for org.w3c.dom.xpath XPathEvaluator evaluate

List of usage examples for org.w3c.dom.xpath XPathEvaluator evaluate

Introduction

In this page you can find the example usage for org.w3c.dom.xpath XPathEvaluator evaluate.

Prototype

public Object evaluate(String expression, Node contextNode, XPathNSResolver resolver, short type, Object result)
        throws XPathException, DOMException;

Source Link

Document

Evaluates an XPath expression string and returns a result of the specified type if possible.

Usage

From source file:ApplyXPathDOM.java

/** Process input args and execute the XPath.  */
public void doMain(String[] args) throws Exception {
    filename = args[0];// ww w. ja v a  2 s. co m
    xpath = args[1];

    if ((filename != null) && (filename.length() > 0) && (xpath != null) && (xpath.length() > 0)) {
        // Tell that we're loading classes and parsing, so the time it 
        // takes to do this doesn't get confused with the time to do 
        // the actual query and serialization.
        System.out.println("Loading classes, parsing " + filename + ", and setting up serializer");

        // Set up a DOM tree to query.
        InputSource in = new InputSource(new FileInputStream(filename));
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setNamespaceAware(true);
        Document doc = dfactory.newDocumentBuilder().parse(in);

        // Set up an identity transformer to use as serializer.
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        // Use the DOM L3 XPath API to apply the xpath expression to the doc.
        System.out.println("Querying DOM using " + xpath);

        // Create an XPath evaluator and pass in the document.
        XPathEvaluator evaluator = new XPathEvaluatorImpl(doc);
        XPathNSResolver resolver = evaluator.createNSResolver(doc);

        // Evaluate the xpath expression
        XPathResult result = (XPathResult) evaluator.evaluate(xpath, doc, resolver,
                XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

        // Serialize the found nodes to System.out.
        System.out.println("<output>");

        Node n;
        while ((n = result.iterateNext()) != null) {
            if (isTextNode(n)) {
                // DOM may have more than one node corresponding to a 
                // single XPath text node.  Coalesce all contiguous text nodes
                // at this level
                StringBuffer sb = new StringBuffer(n.getNodeValue());
                for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) {
                    sb.append(nn.getNodeValue());
                }
                System.out.print(sb);
            } else {
                serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));
            }
            System.out.println();
        }
        System.out.println("</output>");
    } else {
        System.out.println("Bad input args: " + filename + ", " + xpath);
    }
}