Example usage for javax.xml.xpath XPathExpression evaluate

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:Main.java

public static List<Element> SelectElements(Element parent, String expression) {
    List<Element> nodes = new ArrayList<Element>();
    XPath xpath = getXPath();//  ww w. jav a2s.  co  m
    try {
        XPathExpression xpathExpression = xpath.compile(expression);
        NodeList nodeList = (NodeList) xpathExpression.evaluate(parent, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            nodes.add((Element) (nodeList.item(i)));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return nodes;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);//  w w w .j ava  2s.  c  om

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

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

From source file:Main.java

public static NodeList getNodeListFromElement(Document document, String expression)
        throws XPathExpressionException {
    XPath xPath = getxPathFactory().newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
    return nodeList;
}

From source file:Main.java

public static String getXpathExpressionValue(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
    String value = null;/*ww w . ja v  a  2  s  .  c  om*/
    if (valueNode != null)
        value = valueNode.getNodeValue();
    if (value != null) {
        // if the node is a text node - then we trim and (potentially) look for CDATA
        if (valueNode.getNodeType() == Node.TEXT_NODE) {
            value = value.trim();

            // look for CDATA if we got nothing (stupid whitespace)
            if (value.length() == 0) {
                Node siblingForCDATA = valueNode.getNextSibling();
                if (siblingForCDATA.getNodeType() == Node.CDATA_SECTION_NODE) {
                    value = siblingForCDATA.getNodeValue();
                }
            }
        }
    }

    return value;
}

From source file:Main.java

public static Node getNodeByPath(Node node, String path) {
    XPathExpression expr = compile(path);
    try {/*from w w w  .ja  v a  2s  .co m*/
        return (Node) expr.evaluate(node, XPathConstants.NODE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

public static NodeList getNodesByXPath(File file, String xpath) {
    if (!file.isFile() || !file.canRead())
        return null;

    Document document;/*ww w .  j a va 2  s  .c om*/
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    if (document == null) {
        return null;
    }

    Object result;
    try {
        XPath xpathCompiler = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpr = xpathCompiler.compile(xpath);
        result = xPathExpr.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }

    return (NodeList) result;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Returns the xml value.//from   w ww  . j  a  va  2 s. c  om
 * 
 * @param sIn
 * @param sxpath
 * @return
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws XPathExpressionException
 */
public static String XPathValueFromString(String sIn, String sxpath)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    Document doc = loadXMLFromString(sIn);
    XPath xPath = XPathFactory.newInstance().newXPath();
    // XPath Query for showing all nodes value
    XPathExpression expr = xPath.compile(sxpath);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    String sReturn = "";
    for (int i = 0; i < nodes.getLength(); i++) {
        sReturn = nodes.item(i).getNodeValue();
    }
    return sReturn;
}