Example usage for javax.xml.xpath XPathExpression evaluate

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.ambraproject.search.service.DummySOLRMessageSender.java

private Node XPathSingleNodeQuery(Document dom, String statement) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(statement);

    return (Node) expr.evaluate(dom, XPathConstants.NODE);
}

From source file:io.selendroid.nativetests.GetWindowSourceTest.java

private Element findElementByXpath(String expr, String source) throws Exception {
    String xml = JsonXmlUtil.toXml(new JSONObject(source));
    InputSource is = new InputSource(new StringReader(xml));
    XPath xPath = XPathFactory.newInstance().newXPath();

    XPathExpression xpathExpr = xPath.compile(expr);
    return (Element) xpathExpr.evaluate(is, XPathConstants.NODE);
}

From source file:net.firejack.platform.core.config.meta.parse.XMLStreamDescriptorParser.java

private void uidValidation(String xml)
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*from   w w w .j  ava 2  s  .co m*/
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//*/@uid");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    Map<String, Integer> uids = new HashMap<String, Integer>();
    for (int i = 0; i < nodes.getLength(); i++) {
        String uid = nodes.item(i).getNodeValue();
        Integer count = uids.get(uid);
        if (count == null) {
            count = 0;
        }
        count++;
        uids.put(uid, count);
    }
    StringBuilder errorMessage = new StringBuilder();
    for (Map.Entry<String, Integer> entry : uids.entrySet()) {
        if (entry.getValue() > 1) {
            errorMessage.append(" [").append(entry.getKey()).append("]");
        }
    }
    if (errorMessage.length() > 0) {
        throw new BusinessFunctionException(
                "UIDs are not unique in package.xml. Check UIDs:" + errorMessage.toString());
    }
}

From source file:com.rackspace.api.clients.veracode.responses.AppListResponse.java

private Map<String, String> buildAppMap() {
    Map<String, String> applications = new HashMap<String, String>();

    NodeList applicationNodes = null;

    try {//from  w ww .  ja  v  a2  s  .c om
        XPathExpression expression = xpath.compile(XPATH_EXPRESSION);

        applicationNodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }

    if (applicationNodes != null) {
        for (int i = 0; i < applicationNodes.getLength(); i++) {
            Node currentNode = applicationNodes.item(i);

            applications.put(currentNode.getAttributes().getNamedItem("app_name").getNodeValue(),
                    currentNode.getAttributes().getNamedItem("app_id").getNodeValue());
        }
    }

    return applications;
}

From source file:gov.nih.nci.cabio.RESTAPITest.java

/**
 * Returns the text value of the nodes at the given XPath.
 *//*from  w  w  w .j ava2  s . c om*/
private String getValue(Document doc, String path) throws Exception {
    XPathExpression expr = xpath.compile(path);
    Object result = expr.evaluate(doc, XPathConstants.STRING);
    return (String) result;
}

From source file:com.hotwire.test.steps.application.IosApplication.java

public String getExpectedAnalytics(String analyticsParams) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from w  w  w  . j a  va2  s.  c om*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("vertical");
    XPathFactory xpathfactory = XPathFactory.newInstance();
    XPath xpath = xpathfactory.newXPath();

    String paramsToPath = "//" + (analyticsParams.replaceAll(":", "/")) + "/node()";
    XPathExpression expr = xpath.compile(paramsToPath);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < nodes.getLength(); i++) {
        sb.append((nodes.item(i).getNodeName() + "=" + nodes.item(i).getTextContent()).trim());
    }
    String expectedParams = sb.toString().replaceAll("#text=", ";");

    return expectedParams;
}

From source file:com.ehsy.solr.util.SimplePostTool.java

/**
 * Gets all nodes matching an XPath/*from w  w w.ja  v  a  2 s . c  o  m*/
 */
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xp = factory.newXPath();
    XPathExpression expr = xp.compile(xpath);
    return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}

From source file:mupomat.utility.LongLatService.java

public String getXpathValue(Document doc, String strXpath) throws XPathExpressionException {
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(strXpath);
    String resultData = null;/*from  w w  w. j  ava  2s.  c  o  m*/
    Object result4 = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result4;
    for (int i = 0; i < nodes.getLength(); i++) {
        resultData = nodes.item(i).getNodeValue();
    }
    return resultData;

}

From source file:com.microsoft.samples.federation.FederationMetadataDocument.java

private String getSingleNodeText(String xpathStr) {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();

    try {//from w w w . j a  v a2  s .  c o m
        XPathExpression xpe = xpath.compile(xpathStr);
        NodeList nodeList = (NodeList) xpe.evaluate(_fmdDoc, XPathConstants.NODESET);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0).getNodeValue();
        }

    } catch (XPathExpressionException e) {
        //do something here
    }
    return null;
}

From source file:org.jasig.portlet.calendar.service.RoleService.java

/**
 * Do the real work of reading the role list.
 *
 * @return the set of role names.//from  w  w  w.  ja  v  a 2s  .  c  o m
 */
private Set<String> readRolesFromPortletXml() {
    try {
        URL portletXmlUrl = context.getResource(PORTLET_XML_PATH);
        InputSource is = new InputSource(portletXmlUrl.openStream());

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse(is);

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression xPathExpression = xpath.compile(ROLES_XPATH);

        NodeList nodeList = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);

        Set<String> roles = new LinkedHashSet<String>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            String role = nodeList.item(i).getNodeValue();
            roles.add(role);
        }

        return roles;

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException("Error reading roles from portlet.xml", e);
    }
}