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

/**
 * Select only one node what matches given xpath query
 *
 * @param doc        xml document//from w  ww  . ja v a 2 s.  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

/**
 * Performs a xpath query on a document and returns the matching nodelist.
 *
 * @param doc/*w ww.ja  v a 2 s .c  o  m*/
 * @param xpathQuery
 * @return nodelist of matches
 * @throws Exception
 */
public static NodeList query(Document doc, String xpathQuery) throws Exception {
    NodeList result = null;
    // prepare XPath
    XPath xpath = XPathFactory.newInstance().newXPath();

    try {
        // query xml
        result = (NodeList) xpath.evaluate(xpathQuery, doc, XPathConstants.NODESET);
    } catch (XPathExpressionException xpx) {
        throw new Exception("Error evaluating XPath: " + xpx.getMessage(), xpx);
    }
    return result;
}

From source file:Main.java

@GuardedBy("xpathFactoryLock")
public static XPath createNewXPathInstance() {
    XPathFactory factory = null;/* w  ww .j  av a 2s  . c  o m*/

    xpathFactoryLock.lock();
    try {
        factory = XPathFactory.newInstance();
    } finally {
        xpathFactoryLock.unlock();
    }

    return factory.newXPath();
}

From source file:Main.java

public final static XPath getNewXPath() {
    return XPathFactory.newInstance().newXPath();
}

From source file:Main.java

public static NodeList getValueAsNodes(String filePath, String value)
        throws ParserConfigurationException, SAXException, IOException {
    NodeList nodeList = null;/*from  ww  w  . j  a  va  2  s  . c om*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        XPathExpression expr = xpath.compile(value);
        nodeList = (NodeList) expr.evaluate(getDoc(filePath), XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return nodeList;
}

From source file:Main.java

public static String getAttribute(String fileName, String xPathExpression, String attributeName) {
    try {//from w  w w.  ja  v a2  s  .c  om
        Document document = parseXML(new File(fileName));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression xpathExpression = xpath.compile(xPathExpression);
        Node node = (Node) xpathExpression.evaluate(document, NODE);
        return getAttributeValueByName(node, attributeName);
    } catch (Exception e) {
        throw new RuntimeException("Failed to extract element from:" + fileName, e);
    }
}

From source file:Main.java

public static NodeList getNodeList(Node node, String xPathAsString) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xPathAsString);
    return (NodeList) expr.evaluate(node, XPathConstants.NODESET);
}

From source file:Main.java

/**
 * @param node  An XML DOM Tree for query
 * @param query An XPATH query to run against the DOM Tree
 * @param nc    The namespaceContext that maps prefixes to XML namespace
 * @return A list of nodes that result from running the query against
 *         the node.//from  w  w  w .  j a  va  2s.com
 * @throws Exception If anything goes wrong. No error handling for brevity
 */
public static NodeList query(Node node, String query, NamespaceContext nc) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(nc);
    return (NodeList) xpath.evaluate(query, node, XPathConstants.NODESET);
}

From source file:Main.java

/**
 * Gets the XPath factory, creating it if necessary.
 * /*ww w  . j a  v  a 2  s. c  om*/
 * @return the XPath factory.
 */
public static synchronized XPathFactory getXPathFactory() {
    if (xpathFactory == null) {
        xpathFactory = XPathFactory.newInstance();
    }
    return xpathFactory;
}

From source file:Main.java

public static XPathFactory getXPathFactory() {
    return XPathFactory.newInstance();
}