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

时间:2021-08-11 01:34:49

How to get the xpath of the below jaxrs:server element using java? I can't solve it.

如何使用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>

here is the code segment i used

这是我使用的代码段

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 class have all the namespace I need to use I followed this article to do it.

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

1 个解决方案

#1


0  

I tested this in .NET, and I figured XPATH is XPATH, right?

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

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

Let me know if it works.

如果有效,请告诉我。

#1


0  

I tested this in .NET, and I figured XPATH is XPATH, right?

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

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

Let me know if it works.

如果有效,请告诉我。