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 eval(Node doc, String xpath_url) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    return xpathFactory.newXPath().compile(xpath_url).evaluate(doc);
}

From source file:Main.java

private static XPath getXPath() {
    if (xpath == null) {
        xpath = XPathFactory.newInstance().newXPath();
    }
    return xpath;
}

From source file:Main.java

private static XPath getX_PATH() {
    if (X_PATH == null) {
        X_PATH = XPathFactory.newInstance().newXPath();
    }
    return X_PATH;
}

From source file:Main.java

public static String getNodeCount(String nodeName, String xmlString) {
    String response = "";
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {//from  w  ww  .  j a  va 2s  .c  o  m
        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 XPathFactory newXPathFactory() {
    XPathFactory ret = null;//www.  j  ava2 s. c om
    if (ret == null)
        try {
            ret = XPathFactory.newInstance();
        } catch (Exception exception) {
        }
    if (ret == null)
        try {
            new XPathFactoryImpl();
        } catch (Exception exception1) {
        }
    if (ret == null)
        System.out.println("Error: Can't find XPathFactory");
    return ret;
}

From source file:Main.java

public static String getXmlNodeValue(Document doc, String nodePath) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String nodeValue = (String) xpath.evaluate(nodePath, doc, XPathConstants.STRING);
    return nodeValue;
}

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;/*from w w w .  j a  v  a2s  .co m*/
}

From source file:Main.java

public static NodeList executeXpathQuery(Node root, String query) throws TransformerException {

    try {/*from   w  ww. j a  v a  2 s.c  om*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(query);

        Object result = expr.evaluate(root, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        return nodes;
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Node findNode(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();//from   w w  w .ja v  a2s .c o  m

    return (Node) xp.evaluate(xpath, scope, XPathConstants.NODE);
}

From source file:Main.java

public static XPath namespaceAwareXpath(final String prefix, final String nsURI) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    NamespaceContext ctx = new NamespaceContext() {
        @Override/*from w w  w . j  av a2 s .  c o m*/
        public String getNamespaceURI(String aPrefix) {
            if (aPrefix.equals(prefix))
                return nsURI;
            else
                return null;
        }

        @Override
        public Iterator getPrefixes(String val) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }
    };
    xpath.setNamespaceContext(ctx);
    return xpath;
}