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 NodeList getXpathExpressionNodeList(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    NodeList nodes = (NodeList) xpe.evaluate(xprContext, XPathConstants.NODESET);
    return nodes;
}

From source file:Main.java

public static String[] xpathOnString(String q, String stringdoc) {
    try {/*from  w w  w  . j  a v  a 2 s  . c  o m*/
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        // domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes()));

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(q);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        String res[] = new String[nodes.getLength()];
        for (int i = 0; i < nodes.getLength(); i++) {
            // System.out.println(nodes.item(i).toString());
            res[i] = nodes.item(i).getNodeValue();
        }
        return res;
    } catch (Exception e) {
        System.out.println("XPathUtils.xpathOnString:caught:" + e);
        return null;
    }
}

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  .j av  a  2s .  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 XPathExpression buildXPath(String path, Map<String, String> map) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    if (map != null)
        xpath.setNamespaceContext(new NamespaceContext() {

            public Iterator getPrefixes(String namespaceURI) {
                throw new UnsupportedOperationException();
            }//from  w w w. ja v  a2 s  .  c om

            public String getPrefix(String namespaceURI) {
                throw new UnsupportedOperationException();
            }

            public String getNamespaceURI(String prefix) {
                Objects.requireNonNull(prefix);
                if (map.containsKey(prefix))
                    return map.get(prefix);
                return XMLConstants.NULL_NS_URI;
            }
        });

    try {
        return xpath.compile(path);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns an {@link javax.xml.xpath.XPathExpression} from a string.
 *
 * @param xpathString the XPath string// ww  w.j  ava2 s. c o  m
 * @return the {@link javax.xml.xpath.XPathExpression}
 * @throws XPathExpressionException if the given XPath expression is incorrect
 */
public static XPathExpression getXPathExpression(String xpathString) throws XPathExpressionException {

    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    return xpath.compile(xpathString);
}

From source file:Main.java

public static String getNodeTextByXPath(Document doc, String xpath) {
    try {/*w w  w.  j  a  va  2s  .  com*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpathEn = factory.newXPath();
        return xpathEn.evaluate(xpath, doc);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

From source file:Main.java

private static void removeEmptyTextNodes(final Document document) throws XPathExpressionException {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    final NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        final Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }/*from www . ja  va  2s .  c  om*/
}

From source file:Main.java

public static NodeList getNodeListViaXPath(String xpathExpr, String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile(xpathExpr);

    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

    return nl;//from w  w w .j a  v a 2  s . c om
}

From source file:Main.java

/**
 * //from  w w  w.  j a  v a 2  s.  c o m
 * @param xPath
 * @throws Exception - if path invalid.
 */
public static void isLegalXPath(final String xPath) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.compile(xPath);
}

From source file:Main.java

/**
 * Query xml result./*from  w  w  w . j  av  a  2s  .c  om*/
 * 
 * @param doc
 *            the doc
 * @param xpathExpression
 *            the xpath expression
 * @return the object
 */
public static NodeList queryXMLResult(Document doc, String xpathExpression) {
    // Document doc = getDocument(result);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    NodeList XmlResult = null;
    try {
        XPathExpression expr = xpath.compile(xpathExpression);
        XmlResult = evaluateXpath(doc, expr);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return XmlResult;
}