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 String getFirstValueFromXPath(Document parent, String xpath) throws Exception {
    try {//from   w  ww.  j  a va 2s  .c o  m
        XPath xPath = XPathFactory.newInstance().newXPath();
        return (String) xPath.compile(xpath).evaluate(parent, XPathConstants.STRING);
    } catch (XPathExpressionException xpee) {
        throw new Exception(xpee.getMessage());
    }
}

From source file:Main.java

static void init() throws ParserConfigurationException {
    if (_factory == null) {
        _factory = DocumentBuilderFactory.newInstance();
        _factory.setNamespaceAware(false);
    }//w  ww.ja v a  2  s.  co m
    if (_xpath == null) {
        _xpath = XPathFactory.newInstance().newXPath();
    }
    if (_builder == null) {
        _builder = _factory.newDocumentBuilder();
    }
}

From source file:Main.java

static XPath makeXPath() {
    XPathFactory xpfactory = XPathFactory.newInstance();
    return xpfactory.newXPath();
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

public static Node selectSingleNode(Document refNode, String expression) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node outNode = null;/*from  ww  w  . j  a v a  2  s  . c o m*/
    try {
        outNode = (Node) xPath.compile(expression).evaluate(refNode, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return outNode;
}

From source file:Main.java

public static NodeList getNodes(final Document doc, final String query) throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final XPathExpression expr = xpath.compile(query);
    return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static Node xpathToNode(String xpathQuery, Object domObject) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    return (Node) xpath.evaluate(xpathQuery, domObject, XPathConstants.NODE);
}

From source file:Main.java

/**
 * Use an Xpath expression on the given node or document.
 *
 * @param _xpathExpression xpath expression string
 * @param _xmlDocumentOrNode a {@link Document} or {@link Node} object
 * @return NodeList never null/*from   www  .  j a  v a  2 s  .co  m*/
 * @throws IOException on error
 */
public static NodeList applyXpathExpressionToDocument(String _xpathExpression, Node _xmlDocumentOrNode)
        throws IOException {

    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();
    XPathExpression expr = null;
    try {
        expr = xpath.compile(_xpathExpression);
    } catch (XPathExpressionException _ex) {
        throw new IOException(_ex);
    }

    Object result = null;
    try {
        result = expr.evaluate(_xmlDocumentOrNode, XPathConstants.NODESET);
    } catch (Exception _ex) {
        throw new IOException(_ex);
    }

    return (NodeList) result;
}

From source file:Main.java

public static NodeList getNodes(Document doc, String query) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(query);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}