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

/**
 * Get the first node from the specified object using the specified XPath expression.
 *
 * @param obj the object//from w ww. java 2s  .c o  m
 * @param xpathExpression the XPath expression
 * @return the first node from the specified object matched using the specified XPath expression
 */
public static Node getFirstByXPath(Object obj, String xpathExpression) {
    return evaluate(obj, xpathExpression, XPathConstants.NODE);
}

From source file:Main.java

/**
 * getStringListFromXPath/*  w w  w. j  a v a  2 s  .c om*/
 * Gets a list of strings from an xml.
 * @param rootNodeExpression Evaluated on the root of the sqlconfig document to get a single Node.
 * @param listExpression Evaluated on the Node returned by rootNodeExpression, gets a NodeList.
 * @return A list of the text contents of the NodeList returned by evaluating the listExpression.
 */
public static List<String> getStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression,
        String listExpression) {
    synchronized (xpath) {
        try {
            return getStringListFromXPath((Node) xpath.evaluate(rootNodeExpression, doc, XPathConstants.NODE),
                    xpath, listExpression);
        } catch (Exception e) {
            System.err.println("Error evaluating xpath expression: " + rootNodeExpression);
            e.printStackTrace();
        }
        return new Vector<String>();
    }
}

From source file:Main.java

public static String getXpathValue(String xpathStr, Document doc) throws Exception {
    String value = null;/*from w w  w .  ja v a 2 s.  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

/**
 * Same as {@link #asNode(String, Node)} but allows an xpath to be
 * passed in explicitly for reuse./*from   www  .java  2 s . c  o m*/
 */
public static Node asNode(String nodeName, Node node, XPath xpath) throws XPathExpressionException {
    if (node == null)
        return null;
    return (Node) xpath.evaluate(nodeName, node, XPathConstants.NODE);
}

From source file:Main.java

/**
 * Evaluates a XPath expression from a XML node, returning a Node object
 * /* w  w  w.j  av  a  2 s.  c  o  m*/
 * @param expression
 *            A XPath expression
 * @param node
 *            The Node for which this expression should be evaluated
 * @return The result od evaluating the XPath expression to the given Node
 *         or <code>null</code> if an ecxception occured
 */
public static Node evaluateXPathExpressionAndReturnNode(String expression, Node node) {
    try {
        return (Node) xPath.get().evaluate(expression, node, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Applies the specified XPath expression to the given node and returns the
 * first result.//from  w w  w. ja v  a 2s .c o  m
 * 
 * @param node the root element; the given XPath expression is applied to
 *        this node
 * @param path the XPath expression
 * @return the first node corresponding to the specified XPath expression
 * @throws XPathExpressionException
 */
public static Node evalFirst(Node node, String path) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(path);
    final Node result = (Node) expr.evaluate(node, XPathConstants.NODE);
    return result;
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression)
        throws XPathExpressionException {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }//from ww w  . ja  va  2s  .c o m
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;//  w  w  w.j  a  v  a  2 s.com
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

/**
 * Checks for a given element whether it can find an attribute which matches the 
 * XPath expression supplied. Returns {@link Node} if exists.
 * //from  w  w w.  j  av  a 2 s.c o m
 * @param xPathExpression the xPathExpression (required)
 * @param element (required)
 * 
 * @return the Node if discovered (null if not found)
 */
public static Node findFirstAttribute(String xPathExpression, Element element) {
    Node attr = null;
    try {
        XPathExpression expr = compiledExpressionCache.get(xPathExpression);
        if (expr == null) {
            expr = xpath.compile(xPathExpression);
            compiledExpressionCache.put(xPathExpression, expr);
        }
        attr = (Node) expr.evaluate(element, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("Unable evaluate xpath expression", e);
    }
    return attr;
}

From source file:Main.java

/**
 * <p>//from w  w w  .  j a va 2 s . c om
 * Evaluates the given XPath expression in the context of the given item, with
 * the expectation of a node result.
 * </p>
 * 
 * @param xpe
 *          An XPath expression.
 * @param item
 *          A context item.
 * @return The result of the evaluation as a {@link Node} instance.
 * @throws XPathExpressionException
 *           if the evaluation fails
 * @since 1.67.5
 * @see XPathConstants#NODE
 */
public static Node evaluateNode(XPathExpression xpe, Object item) throws XPathExpressionException {
    return (Node) xpe.evaluate(item, XPathConstants.NODE);
}