Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:Main.java

/**
 * Select only one node what matches given xpath query
 *
 * @param doc        xml document/*from www . j a  va  2s .  co m*/
 * @param expression xpath query
 * @return first element which confirms given xpath query.
 * @throws XPathExpressionException in case of any errors.
 */
public static Element queryElement(final Document doc, final String expression)
        throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    return (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
}

From source file:Main.java

public static String orEmptyStr(XPathExpression xpe, Node n) throws XPathExpressionException {
    Node inner = (Node) xpe.evaluate(n, XPathConstants.NODE);
    return inner == null ? "" : inner.getTextContent();
}

From source file:Main.java

public static Node selectSingleNode(Document refNode, String expression) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node outNode = null;//  www . j av  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 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

public static Node xpathNode(Node node, String expression) throws XPathExpressionException {
    return (Node) evaluateXpath(expression, node, XPathConstants.NODE, null);
}

From source file:Main.java

public static Node seek(String path, Node e) {
    return (Node) evaluateXPath(path, e, XPathConstants.NODE);
}

From source file:Main.java

public static String peekValue(final Document doc, final String xpathExpression)
        throws XPathExpressionException {
    final XPath xPath = XPF.newXPath();
    final XPathExpression expression = xPath.compile(xpathExpression);

    final Node node = (Node) expression.evaluate(doc, XPathConstants.NODE);
    final String result = (node != null) ? node.getTextContent() : null;
    return result;
}

From source file:Main.java

/**
 * Get node from XML document using xpath expression
 * //w ww . j  a v  a 2s  .c o m
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Node getNode(final Document doc, final String expression) throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    return (Node) result;
}

From source file:Main.java

/**
 * Get one element from XML document using xpath expression
 * /*from  w w  w  .j a  v a 2s . c o  m*/
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Element getElement(final Document doc, final String expression) throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    return (Element) result;
}

From source file:Main.java

public static Node queryNode(Document document, String xPath) {
    try {//from  w  w w.  j av a2 s  .  co m
        XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath);
        return (Node) expr.evaluate(document, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException(e);
    }
}