Example usage for javax.xml.xpath XPath evaluate

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.trustedanalytics.hadoop.admin.tools.HadoopClientParamsImporter.java

static Optional<Map<String, String>> scanConfigZipArchive(InputStream source)
        throws IOException, XPathExpressionException {

    InputStream zipInputStream = new ZipInputStream(new BufferedInputStream(source));
    ZipEntry zipFileEntry;//w  w  w.  j a  v a  2  s  .  c o  m
    Map<String, String> ret = new HashMap<>();
    while ((zipFileEntry = ((ZipInputStream) zipInputStream).getNextEntry()) != null) {
        if (!zipFileEntry.getName().endsWith("-site.xml")) {
            continue;
        }
        byte[] bytes = IOUtils.toByteArray(zipInputStream);
        InputSource is = new InputSource(new ByteArrayInputStream(bytes));
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate(CONF_PROPERTY_XPATH, is, XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node propNode = nodeList.item(i);
            String key = (String) xPath.evaluate("name/text()", propNode, XPathConstants.STRING);
            String value = (String) xPath.evaluate("value/text()", propNode, XPathConstants.STRING);
            ret.put(key, value);
        }
    }
    return Optional.of(ret);
}

From source file:Main.java

/**
 * Extract a String node from an XML Message
 *
 * @param xpath     XPath object//from  ww  w.j a  v  a 2  s  .c  o m
 * @param nodePath  XPath statement to locate the node
 * @param xmlString Xml string object to extract the data from
 * @return The requested data, or "" if not found.
 */
public static String extractNode(XPath xpath, String nodePath, String xmlString) {
    InputSource inputSource = new InputSource(new StringReader(xmlString));

    try {
        return (String) xpath.evaluate(nodePath, inputSource, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        return "";
    }
}

From source file:org.orcid.examples.jopmts.mvc.OrcidWork.java

public static List<Model> parseAuthors(XPath xpath, Node orcidDocument) throws XPathExpressionException {
    NodeList authors = (NodeList) xpath.evaluate("o:work-contributors/o:contributor", orcidDocument,
            XPathConstants.NODESET);
    ArrayList<Model> authList = new ArrayList<Model>();
    for (int i = 0; i < authors.getLength(); i++) {
        Node author = authors.item(i);
        Model authorModel = new OrcidAuthor(author, xpath);
        authList.add(authorModel);/*from   www . j  a va 2s  .  c o  m*/
    }
    return authList;
}

From source file:Main.java

public static NodeList getNodesByPath(String path, Element localElement, Document doc)
        throws XPathExpressionException {
    // Note: if using absolute path, then the root element must also be specified,
    // that is, it should be like "/clinical_studies/clinical_study/..."
    XPath xpath = factory.newXPath();
    Object element = path.startsWith("/") || localElement == null ? doc : localElement;
    NodeList nodeList = (NodeList) xpath.evaluate(path, element, XPathConstants.NODESET);
    return nodeList;
}

From source file:Main.java

public static final String indenting(Node rootNode) throws XPathExpressionException, TransformerException {

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", rootNode,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }/*from  www  .j  a v  a2  s .co  m*/

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(rootNode), streamResult);

    return stringWriter.toString();
}

From source file:ch.entwine.weblounge.bridge.oaipmh.harvester.OaiPmhResponse.java

public static String xpathString(XPath xpaht, Node context, String expr) {
    try {/*from  w  ww  . j  ava2 s . c o m*/
        return ((String) xpaht.evaluate(expr, context, XPathConstants.STRING)).trim();
    } catch (XPathExpressionException e) {
        throw new RuntimeException("malformed xpath expression " + expr, e);
    }
}

From source file:Main.java

private static NodeList getNodeList(Document document, String expression) throws XPathExpressionException {

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(),
            XPathConstants.NODESET);

    return nodeList;
}

From source file:ch.entwine.weblounge.bridge.oaipmh.harvester.OaiPmhResponse.java

public static NodeList xpathNodeList(XPath xpath, Node context, String expr) {
    try {/*w w w.j a v a 2 s.  c  o m*/
        return (NodeList) xpath.evaluate(expr, context, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("malformed xpath expression " + expr, e);
    }
}

From source file:Main.java

private static NodeList getNodeList(String xmlContent, String expression)
        throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {

    Document document = parse(xmlContent, CHARSET_UTF8);
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(),
            XPathConstants.NODESET);

    return nodeList;
}

From source file:Main.java

/**
 * Get the W3C NodeList instance associated with the XPath selection
 * supplied.// ww w.  ja va 2 s.  c  om
 *
 * @param node  The document node to be searched.
 * @param xpath The XPath String to be used in the selection.
 * @return The W3C NodeList instance at the specified location in the
 *         document, or null.
 */
public static NodeList getNodeList(Node node, String xpath) {
    if (node == null) {
        throw new IllegalArgumentException("null 'document' arg in method call.");
    } else if (xpath == null) {
        throw new IllegalArgumentException("null 'xpath' arg in method call.");
    }
    try {
        XPath xpathEvaluater = xPathFactory.newXPath();

        if (xpath.endsWith(ELEMENT_NAME_FUNC)) {
            return (NodeList) xpathEvaluater.evaluate(
                    xpath.substring(0, xpath.length() - ELEMENT_NAME_FUNC.length()), node,
                    XPathConstants.NODESET);
        } else {
            return (NodeList) xpathEvaluater.evaluate(xpath, node, XPathConstants.NODESET);
        }
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("bad 'xpath' expression [" + xpath + "].");
    }
}