Example usage for javax.xml.xpath XPath setNamespaceContext

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

Introduction

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

Prototype

public void setNamespaceContext(NamespaceContext nsContext);

Source Link

Document

Establish a namespace context.

Usage

From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java

/**
 * @param responseString//ww w  .ja  va2  s  .c  om
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws XPathExpressionException
 */
public static HashMap<String, HashMap<String, String>> responseFactory(String responseString)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

    HashMap<String, HashMap<String, String>> returnMap = new HashMap<String, HashMap<String, String>>();

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    Document document = documentBuilder.parse(new InputSource(new StringReader(responseString)));

    //String messageName = document.getFirstChild().getNodeName();

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

    xpath.setNamespaceContext(new SugarCRMNamespaceContext());
    XPathExpression expr = xpath.compile("//ns1:get_entry_listResponse/return/entry_list/item");

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

    for (int i = 0; i < nodes.getLength(); i++) {

        NodeList innerNodes = nodes.item(i).getChildNodes();

        String id = innerNodes.item(0).getTextContent();

        HashMap<String, String> elementData = new HashMap<String, String>();

        NodeList infoNodes = innerNodes.item(2).getChildNodes();

        for (int z = 0; z < infoNodes.getLength(); z++) {
            String name = infoNodes.item(z).getFirstChild().getTextContent();
            String value = infoNodes.item(z).getLastChild().getTextContent();

            elementData.put(name, value);
        }
        returnMap.put(id, elementData);
    }
    return returnMap;
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageTemplateImpl.java

/**
 * Initializes this page template from an XML node that was generated using
 * {@link #toXml()}./*  ww w  .j  av  a 2 s. c o  m*/
 * <p>
 * To speed things up, you might consider using the second signature that uses
 * an existing <code>XPath</code> instance instead of creating a new one.
 * 
 * @param node
 *          the page template node
 * @throws IllegalStateException
 *           if the page template cannot be parsed
 * @see #fromXml(Node, XPath)
 * @see #toXml()
 */
public static PageTemplate fromXml(Node node) throws IllegalStateException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Define the xml namespace
    xpath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            return "ns".equals(prefix) ? SiteImpl.SITE_XMLNS : null;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        public Iterator<?> getPrefixes(String namespaceURI) {
            return null;
        }
    });

    return fromXml(node, xpath);
}

From source file:Main.java

public static XPath namespaceAwareXpath(final String prefix, final String nsURI) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    NamespaceContext ctx = new NamespaceContext() {
        @Override/*from   ww  w. java  2s  .c  o m*/
        public String getNamespaceURI(String aPrefix) {
            if (aPrefix.equals(prefix))
                return nsURI;
            else
                return null;
        }

        @Override
        public Iterator getPrefixes(String val) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }
    };
    xpath.setNamespaceContext(ctx);
    return xpath;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Make ns aware xpath.//from   www .java  2s  . c o m
 * 
 * @return the x path
 */
public static XPath makeNSAwareXpath() {
    if (xpfactory == null) {
        xpfactory = XPathFactory.newInstance();
    }
    XPath xpath = xpfactory.newXPath();
    xpath.setNamespaceContext(nsContext);
    return xpath;
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser.java

private static XPath newXpath() {
    XPathFactory xf = XPathFactory.newInstance();
    XPath xpath = xf.newXPath();

    NamespaceContextImpl ctx = new NamespaceContextImpl();

    ctx.addNamespace("ovf", "http://schemas.dmtf.org/ovf/envelope/1");
    ctx.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    ctx.addNamespace("rasd",
            "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData");
    ctx.addNamespace("vssd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData");
    ctx.addNamespace("vmw", "http://www.vmware.com/schema/ovf");

    xpath.setNamespaceContext(ctx);
    return xpath;
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * @param xpathString XPath query string
 * @return/*  ww w.java2 s  . com*/
 */
private static XPathExpression createXPathExpression(final String xpathString) {

    /* XPath */
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(namespacePrefixMapper);
    try {
        final XPathExpression expr = xpath.compile(xpathString);
        return expr;
    } catch (XPathExpressionException ex) {
        throw new DSSException(ex);
    }
}

From source file:ch.entwine.weblounge.common.impl.site.ModuleImpl.java

/**
 * Initializes this module from an XML node that was generated using
 * {@link #toXml()}./*from w  ww. j a  v  a  2  s.  c  o m*/
 * <p>
 * To speed things up, you might consider using the second signature that uses
 * an existing <code>XPath</code> instance instead of creating a new one.
 * 
 * @param config
 *          the module node
 * @throws IllegalStateException
 *           if the module cannot be parsed
 * @see #fromXml(Node, XPath)
 * @see #toXml()
 */
public static Module fromXml(Node config) throws IllegalStateException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Define the xml namespace
    XPathNamespaceContext nsCtx = new XPathNamespaceContext(false);
    nsCtx.defineNamespaceURI("m", MODULE_XMLNS);
    xpath.setNamespaceContext(nsCtx);

    return fromXml(config, xpath);
}

From source file:net.javacrumbs.springws.test.common.XPathExpressionEvaluator.java

public String evaluateExpression(Document document, String expression, URI uri,
        NamespaceContext namespaceContext) {
    XPathFactory factory = XPathFactory.newInstance();
    factory.setXPathVariableResolver(new WsTestXPathVariableResolver(uri));
    try {/*from w  w  w .j av  a  2 s.  co  m*/
        XPath xpath = factory.newXPath();
        if (namespaceContext != null) {
            xpath.setNamespaceContext(namespaceContext);
        }
        String result = xpath.evaluate(expression, document);
        logger.debug("Expression \"" + expression + "\" resolved to \"" + result + "\"");
        return result;
    } catch (XPathExpressionException e) {
        throw new ExpressionResolverException(
                "Could not evaluate XPath expression \"" + expression + "\" : \"" + e.getMessage() + "\"", e);
    }
}

From source file:org.openmrs.module.dhisreport.api.dxf2.DataValueSetTest.java

protected String xpathTest(String xpathString, String xml) throws XPathExpressionException {
    InputSource source = new InputSource(new StringReader(xml));
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new Dxf2NamespaceResolver());

    return (String) xpath.evaluate(xpathString, source);
}

From source file:com.rest4j.generator.DocGeneratorTest.java

@Test
public void testPreprocess() throws Exception {
    Document xml = getDocument("doc-generator-graph.xml");
    gen.preprocess(xml);/* www .  ja  va 2s.com*/
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    xpath.setNamespaceContext(new Generator.APINamespaceContext());

    assertEquals("patch A,patch B,B,patch C,C",
            modelsToString(xpath.compile("//api:endpoint[api:service/@method='patch']/api:body/api:model")
                    .evaluate(xml, XPathConstants.NODESET)));
    assertEquals("C",
            modelsToString(xpath.compile("//api:endpoint[api:service/@method='patch']/api:response/api:model")
                    .evaluate(xml, XPathConstants.NODESET)));
    assertEquals("B,C", modelsToString(xpath.compile("//api:endpoint[api:route='get']/api:response/api:model")
            .evaluate(xml, XPathConstants.NODESET)));
    assertEquals("B,C", modelsToString(xpath.compile("//api:endpoint[api:route='get']/api:response/api:model")
            .evaluate(xml, XPathConstants.NODESET)));
    assertEquals("", modelsToString(xpath.compile("//api:endpoint[api:route='binary']/api:response/api:model")
            .evaluate(xml, XPathConstants.NODESET)));
}