Example usage for javax.xml.xpath XPathFactory newInstance

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

Introduction

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

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:Main.java

public static XPath createXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    return factory.newXPath();
}

From source file:Main.java

public static String evaluate(File xmlFile, String xPathExpression) {
    String result = null;//from   w  w w.  ja  v  a 2s .com

    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

/**
 * Returns a single node that matches the given XPath expression.
 * /* ww  w  .  jav  a2 s. c  o m*/
 * @param doc
 *            Document that contains the nodes.
 * @param expression
 *            XPath expression to be matched.
 * @return Returns a single node matching the given expression.
 */
public static Node selectSingleNode(Document doc, String expression) {
    try {
        XPath xpath = XPathFactory.newInstance().newXPath();

        return (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        // ignore
    }

    return null;
}

From source file:Main.java

/**
 * Uses the passed XPath expression to locate a single node in the passed element.
 * @param targetNode The node to search.
 * @param expression The XPath expression.
 * @return The located node or null if one is not found.
 *///  w w  w .  j  a  va2s.c  om
public static Node xGetNode(Node targetNode, String expression) {
    Node node = null;
    XPath xpath = null;
    try {
        xpath = XPathFactory.newInstance().newXPath();
        XPathExpression xpathExpression = xpath.compile(expression);
        node = (Node) xpathExpression.evaluate(targetNode, NODE);
        return node;
    } catch (Exception e) {
        throw new RuntimeException("XPath:Failed to locate the node:" + expression, e);
    }
}

From source file:Main.java

public static Object selectObject(Document xmlDocument, String expression, QName returnType)
        throws XPathExpressionException {
    XPath xPath = XPathFactory.newInstance().newXPath();

    return xPath.compile(expression).evaluate(xmlDocument, returnType);
}

From source file:Main.java

/**
 * Select node list what matches given xpath query
 *
 * @param doc        xml document/*from ww  w.  j a v a 2  s . c o  m*/
 * @param expression xpath query
 * @return nodes which confirms given xpath query.
 * @throws XPathExpressionException in case of any errors.
 */
public static NodeList query(final Document doc, final String expression) throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    return (NodeList) xpath.evaluate(expression, doc.getDocumentElement(), XPathConstants.NODESET);
}

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);
    }/*  ww w  . j a  v  a  2s.com*/

    return xpath.evaluate(expression, node, returnType);
}

From source file:Main.java

public static Object getXPath(Node node, String xPathString, QName returnType) throws XPathExpressionException {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    XPathExpression expression = xPath.compile(xPathString);
    return expression.evaluate(node, returnType);
}

From source file:Main.java

public static String getValueAsString(String filePath, String value)
        throws ParserConfigurationException, SAXException, IOException {
    String id = null;/* www  .  j  a  v a 2s. c  o m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        XPathExpression expr = xpath.compile(value);
        id = (String) expr.evaluate(getDoc(filePath), XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return id;
}

From source file:Main.java

public static Object execXpathGetNodeList(String srcXmlString, String xPath) {
    Object result = null;/*from   www .j  a  v a  2s . c om*/
    try {
        Document doc = stringToDoc(srcXmlString);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);
        result = expr.evaluate(doc, XPathConstants.NODESET);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return result;
}