Example usage for javax.xml.xpath XPathFactory newInstance

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

Introduction

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

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:Main.java

public static NodeList getChildNodesByExpression(Document doc, String expression) {
    try {/*from www  . ja  va2 s.c o  m*/
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(expression);

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

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static XPath newXPath() {
    return XPathFactory.newInstance().newXPath();
}

From source file:Main.java

public static List<String> xPathEvalList(String content, String expression) throws XPathExpressionException {
    List<String> retValues = new ArrayList<String>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(content));
    NodeList ox = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
    for (int i = 0; i < ox.getLength(); i++) {
        retValues.add(ox.item(i).getNodeValue());
    }/*from   w w  w  .j a v a 2  s .  c  o m*/

    return retValues;
}

From source file:Main.java

/***
 * Returns the string value of evaluated xpath
 *
 * @param  document    - XML Document to check
 * @param  xpathString - xpath to evaluate
 * @return String evaluation of the xpath
 *//*from   www. ja  v a  2 s . c o m*/
public static String getValueFromXPath(Document document, String xpathString) {
    try {
        return (String) XPathFactory.newInstance().newXPath().evaluate(xpathString, document,
                XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Error evaluating xpath [" + xpathString + "] -- " + e.getMessage());
    }
}

From source file:Main.java

public static Node getChildNode(Document doc, String parentTag, String childnode) {
    try {/*  ww w .j a v a  2  s  .c  om*/
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("//" + parentTag + "/" + childnode);
        Object obj = expr.evaluate(doc, XPathConstants.NODE);
        return obj != null ? (Node) obj : null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object xpathFind(Document doc, String xpath, QName returnType) {
    try {// ww  w.j a v a2 s.c o  m
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpathToolkit = xpathFactory.newXPath();
        XPathExpression xpathExpression = xpathToolkit.compile(xpath);
        return xpathExpression.evaluate(doc, returnType);
    } catch (Exception e) {
        throw new RuntimeException("Error finding nodes for <" + xpath + ">", e);
    }
}

From source file:Main.java

public static Node getXpathExpressionNode(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    Node node = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
    return node;/*w w w .ja  v  a 2s. co  m*/
}

From source file:Main.java

public static XPath newXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    return xpath;
}

From source file:Main.java

public static List<Node> xPathEvalListNodes(String content, String expression) throws XPathExpressionException {
    List<Node> retValues = new ArrayList<Node>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(content));
    NodeList ox = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
    for (int i = 0; i < ox.getLength(); i++) {
        retValues.add(ox.item(i));//w  ww. j  a  v a2s . c  om
    }

    return retValues;
}

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;
}