Example usage for javax.xml.xpath XPath evaluate

List of usage examples for javax.xml.xpath XPath evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPath evaluate.

Prototype

public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate an XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:Main.java

private static Object getValue(final String xpathExpression, final Node node, final QName type,
        final NamespaceContext nsContext) throws XPathExpressionException {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(nsContext);
    return xpath.evaluate(xpathExpression, node, type);
}

From source file:Main.java

public static Node selectSingleNode(String express, Object source) {
    Node result = null;// w ww  .  jav  a  2  s .  com
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String getXpathValue(String xpathStr, Document doc) throws Exception {
    String value = null;/* w  ww.j ava2s  . c  o m*/
    try {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element e = (Element) xpath.evaluate(xpathStr, doc, XPathConstants.NODE);
        value = e.getTextContent();
    } catch (Exception e) {
        throw e;
    }
    return value;
}

From source file:Main.java

public static Node selectSingleNode(final Node sourceNode, final String xPathExpression) {
    Node result;/*www  .  ja  va  2 s.co  m*/
    XPathFactory factory = XPathFactory.newInstance(); // http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
    XPath xPathParser = factory.newXPath();

    try {
        result = (Node) xPathParser.evaluate(xPathExpression, sourceNode, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        result = null;
    }

    return result;
}

From source file:Main.java

public static Double getNodesCount(String expression, Document document) {
    XPath xpath = xPathFactory.newXPath();
    try {//from  w ww  . j a v  a 2s  .c o m
        Double count = (Double) xpath.evaluate("count(" + expression + ")", document, XPathConstants.NUMBER);
        return count;
    } catch (XPathExpressionException xpe) {
        throw new IllegalStateException(xpe);
    }
}

From source file:Main.java

public static NodeList selectNodes(String express, Object source) {
    NodeList result = null;//w  ww. j ava2 s . c o  m
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Object getByXPath(Node parentNode, NamespaceContext context, String xPathExpression, QName type)
        throws XPathExpressionException {
    XPathFactory xPathFactory = xPathFactoryCache.get();
    XPath xPath = xPathFactory.newXPath();
    xPath.setNamespaceContext(context);/*w  w w.ja  v a 2 s. c  om*/
    return xPath.evaluate(xPathExpression, parentNode, type);
}

From source file:Main.java

/**
 * Evaluates the XPath expression against the <code>xml</code> and returns the selected nodes.
 * //from   ww w  . ja  v a2 s  .  c o  m
 * @param expression Expression to evaluate.
 * @param xml The xml to query.
 * @return The selected nodes.
 * @throws XPathExpressionException If an error occurs evaluating the expression.
 */
public static NodeList getNodes(final String expression, final String xml) throws XPathExpressionException {
    final InputSource source = new InputSource(new StringReader(xml));
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    return (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET);
}

From source file:Main.java

private static void removeEmptyTextNodes(final Document document) throws XPathExpressionException {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    final NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        final Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }//from   w w w .j  av a2 s  . c o  m
}

From source file:Main.java

public static Node selectSingleNode(Node node, String express) {
    Node result = null;/* w  w w.j av  a2 s .  c  o  m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, node, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}