Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:Main.java

public static XPathExpression createXpath(final String xpath) throws XPathExpressionException {
    final XPathFactory xPathfactory = XPathFactory.newInstance();
    final XPath x = xPathfactory.newXPath();
    final XPathExpression expr = x.compile(xpath);

    return expr;/*ww  w  .  j  a  v a  2  s.co  m*/
}

From source file:Main.java

public static Object getXPath(Node node, String xPathString, QName returnType) throws XPathExpressionException {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    XPathExpression expression = xPath.compile(xPathString);
    return expression.evaluate(node, returnType);
}

From source file:Main.java

/**
 * Returns an {@link javax.xml.xpath.XPathExpression} from a string.
 *
 * @param xpathString the XPath string/* w  w  w  .jav a 2s  . c o  m*/
 * @return the {@link javax.xml.xpath.XPathExpression}
 * @throws XPathExpressionException if the given XPath expression is incorrect
 */
public static XPathExpression getXPathExpression(String xpathString) throws XPathExpressionException {

    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    return xpath.compile(xpathString);
}

From source file:Main.java

public static NodeList evalXpath(InputStream xmlStream, String path) throws XPathExpressionException {
    InputSource inXML = new InputSource(xmlStream);
    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();
    XPathExpression expr = xpath.compile(path);
    Object result = expr.evaluate(inXML, XPathConstants.NODESET);
    return (NodeList) result;
}

From source file:Main.java

public static Object evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext)
        throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    if (nsContext != null) {
        xpath.setNamespaceContext(nsContext);
    }/*from   w w  w .jav a 2s  .c o  m*/

    return xpath.evaluate(expression, node, returnType);
}

From source file:Main.java

public static String getValueByXpath(Node doc, String xquery) {
    try {// w  ww .  ja  v a2 s .com
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expression = xpath.compile(xquery);
        String result = (String) expression.evaluate(doc, XPathConstants.STRING);
        return result;
    } catch (Exception e) {

    }
    return null;
}

From source file:Main.java

private static XPath createXpath() {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    return xpath;
}

From source file:Main.java

public static String getNodeCount(String nodeName, String xmlString) {
    String response = "";
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {/*www .j  a  v  a 2  s.c om*/
        XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")");
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlString));
        response = xPathExpression.evaluate(is);

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return response;
}

From source file:Main.java

public static NodeList getElementsByXpath(Document docInput, String xPath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(xPath);
    return ((NodeList) expr.evaluate(docInput, XPathConstants.NODESET));
}

From source file:Main.java

public static NodeList getNodeListViaXPath(String xpathExpr, String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile(xpathExpr);

    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

    return nl;/*from   ww  w.  ja  va2s.  co  m*/
}