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 XPathExpression compileExpression(final String xQuery) {
    if ((xQuery != null) && (xQuery.length() > 0)) {
        try {/*from  ww  w. j  a  va  2 s  .  c  o m*/
            return XPathFactory.newInstance().newXPath().compile(xQuery);
        } catch (final XPathExpressionException e) {
            throw new RuntimeException(e);
        }
    }
    throw new RuntimeException("Query is null!");
}

From source file:Main.java

public static NodeList getNodes(Node doc, String xpath_url) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    return (NodeList) xpathFactory.newXPath().compile(xpath_url).evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static Node getXmlNodeForExpression(String expression, Node widgetNode) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    return (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE);

}

From source file:Main.java

public static NodeList selectNodeList(Node xml, String xPath) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xPath);
    NodeList nodeList = (NodeList) expr.evaluate(xml, XPathConstants.NODESET);
    return nodeList;
}

From source file:Main.java

public static Node selectSingleNode(Node refNode, String expression) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node outNode = null;/*from ww w.  j a  va  2 s. c om*/
    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 XPathFactory newXPathFactory() {
    factoryLock.lock();/*www . j  av a  2 s. c om*/
    try {
        return XPathFactory.newInstance();
    } finally {
        factoryLock.unlock();
    }
}

From source file:Main.java

public static Node findSchemaPropertyByDimensionName(Document xmlDoc, String xPathExpression) {
    XPath xPath = XPathFactory.newInstance().newXPath();

    Node nOde = null;/* w  ww.ja v  a  2s . c o  m*/
    try {
        nOde = (Node) xPath.compile(xPathExpression).evaluate(xmlDoc, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return nOde;
}

From source file:Main.java

private static XPathExpression createXPathExpression(final String expression) throws XPathExpressionException {
    assert expression != null;
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xpath = xFactory.newXPath();
    XPathExpression expr = xpath.compile(expression);
    return expr;/*from   ww w  .j  a v a  2 s .  co m*/
}

From source file:Main.java

public static String getValueByXpath(Node doc, String xquery) {
    try {//from ww w .j  a va2 s  .  co  m
        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

public static NodeList getNodesByXPath(Node node, String xpathStr) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr;// w  w  w .  ja v  a  2 s. c  o m
    try {
        expr = xpath.compile(xpathStr);
        NodeList favoris = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
        return favoris;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}