Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:Main.java

public static String evaluate(File xmlFile, String xPathExpression) {
    String result = null;/*from   w  ww.  j  a va 2  s . c  om*/

    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        result = xPath.evaluate(xPathExpression, new InputSource(new FileInputStream(xmlFile)));
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return result;
}

From source file:Main.java

public static Object evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext)
        throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    if (nsContext != null) {
        xpath.setNamespaceContext(nsContext);
    }/*  w  w w  .ja  v a 2s  .  c  om*/
    return xpath.evaluate(expression, node, returnType);
    // XPathExpression xpathExpr = xpath.compile(expression);
    // return xpathExpr.evaluate(node, returnType);
}

From source file:Main.java

public static List<Element> getXPathList(Node node, String expr) {
    assertNotNull("node == null", node);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    List<Element> result = new ArrayList<Element>();
    try {//from   w ww  . j a v  a 2  s . c  om
        NodeList nl = (NodeList) xpath.evaluate(expr, node, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            result.add((Element) nl.item(i));
        }
        return result;
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Applies the specified XPath expression to the given node and returns the
 * first result.//from  w w  w.  j ava2s.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

public static String evalXPathAsString(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    return (String) expr.evaluate(item, XPathConstants.STRING);
}

From source file:Main.java

public static Object xpathFind(Document doc, String xpath, QName returnType) {
    try {/*w w  w  .j  a v  a  2  s . com*/
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpathToolkit = xpathFactory.newXPath();
        XPathExpression xpathExpression = xpathToolkit.compile(xpath);
        return xpathExpression.evaluate(doc, returnType);
    } catch (Exception e) {
        throw new RuntimeException("Error finding nodes for <" + xpath + ">", e);
    }
}

From source file:Main.java

public static NodeList evalXPathAsNodeList(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    return (NodeList) expr.evaluate(item, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList SelectNodes(Document doc, String xmlpath) {
    NodeList nodes = null;/*from www  .  j av  a  2s  .co m*/
    Object result;
    try {
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();

        XPathExpression expr;
        expr = xpath.compile(xmlpath);
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return nodes;
}

From source file:Main.java

public static int evalXPathAsInt(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    Number result = (Number) expr.evaluate(item, XPathConstants.NUMBER);
    return result == null ? 0 : result.intValue();
}

From source file:Main.java

/**
 * // w w  w. j a  v  a 2 s . c  o  m
 * <B>Purpose:</B> Evaluates an XPath and returns the Node list associated
 * witht the XPath
 * 
 * @param element
 * @param xpathstring
 * @return
 * @throws XPathExpressionException
 */
public static NodeList evaluateXPath(Element element, String xpathstring) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(xpathstring);
    NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET);

    return result;
}