Java XPath Create xpath(String xml, String xpath)

Here you can find the source of xpath(String xml, String xpath)

Description

xpath

License

Apache License

Declaration

public static String xpath(String xml, String xpath) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Main {
    public static String xpath(String xml, String xpath) throws Exception {
        XPath xp = XPathFactory.newInstance().newXPath();
        Document doc = parse(xml);
        return xp.evaluate(xpath, doc.getDocumentElement());
    }/*from   www .jav  a2  s .c o m*/

    public static Document parse(String xml) throws Exception {
        return newBuilder().parse(new InputSource(new StringReader(xml)));
    }

    public static DocumentBuilder newBuilder() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);
        return dbf.newDocumentBuilder();
    }
}

Related

  1. newXpathExpression(final XPath xpath, final String expression)
  2. newXPathFactory()
  3. xPath()
  4. xpath()
  5. xpath(String expression)
  6. xPath(String xPath)
  7. xpathNode(Node node, String xpath)
  8. xpathNodes(Node node, String expression)
  9. xPathNodes(String expr, Object context)