如何使用java获取namespace属性的xpath

时间:2015-04-29 00:36:40

标签: java xml xpath jax-rs jax-ws

如何使用java获取以下jaxrs:server元素的xpath?我无法解决它。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
    <bean id="discoverRSByeBean" class="discovery.hello.DiscoverRSBye"/>
    <jaxrs:server id="discoverRSBye" address="/discover_r_s_bye">
        <jaxrs:serviceBeans>
            <ref bean="discoverRSByeBean"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="DiscoveryRSHelloBean" class="discovery.hello.DiscoveryRSHello"/>
    <jaxrs:server id="DiscoveryRSHello" address="/discovery_r_s_hello">
        <jaxrs:serviceBeans>
            <ref bean="DiscoveryRSHelloBean"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
</beans>

这是我使用的代码段

InputStream configStream = getConfigLocation(context.getServletContext());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(configStream);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
xpath.setNamespaceContext(new HardcodedNamespaceResolver());
NodeList nodeList = (NodeList) xpath.evaluate("beans/jaxrs:server", doc, XPathConstants.NODESET);

HardcodedNamespaceResolver类具有我需要使用的所有命名空间我遵循this文章来执行此操作。

1 个答案:

答案 0 :(得分:0)

我在.NET中对此进行了测试,我认为XPATH是XPATH,对吧?

"//*[namespace-uri(.)='http://cxf.apache.org/jaxrs']"

让我知道它是否有效。