2013-10-11 158 views
1

我有這樣的WSDLXPATH檢索元素

<definitions targetNamespace="http://testwork/" name="HelloWorldService" 
      xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
      xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" 
      xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
      xmlns:tns="http://testwork/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
    <types> 
     <xsd:schema> 
      <xsd:import namespace="http://soapServlet/" schemaLocation="http://localhost:8085/testwork/soapServlet?xsd=1"/> 
     </xsd:schema> 
    </types> 
    <message name="sayHelloWorldFrom"> 
     <part name="parameters" element="tns:sayHelloWorldFrom"/> 
    </message> 
    <message name="sayHelloWorldFromResponse"> 
     <part name="parameters" element="tns:sayHelloWorldFromResponse"/> 
    </message> 
    <message name="additionalFault"> 
     <wsdl:part name="error" element="tns:responseFault"/> 
    </message> 
    <portType name="HelloWorld"> 
     <operation name="sayHelloWorldFrom"> 
      <input wsam:Action="http://testwork/HelloWorld/sayHelloWorldFromRequest" message="tns:sayHelloWorldFrom"/> 
      <output wsam:Action="http://testwork/HelloWorld/sayHelloWorldFromResponse" message="tns:sayHelloWorldFromResponse"/> 
      <wsdl:fault name="error" message="additionalFault"/> 
     </operation> 
    </portType> 
    <binding name="HelloWorldPortBinding" type="tns:HelloWorld"> 
     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
     <operation name="sayHelloWorldFrom"> 
      <soap:operation soapAction=""/> 
      <input> 
       <soap:body use="literal"/> 
      </input> 
      <output> 
       <soap:body use="literal"/> 
      </output> 
      <fault name="error"> 
       <soap:fault name="error" use="literal"/> 
      </fault> 
     </operation> 
    </binding> 
    <service name="HelloWorldService"> 
     <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"> 
      <soap:address location="http://localhost:8085/testwork/soapServlet"/> 
     </port> 
    </service> 
</definitions> 

我要檢查,如果這個元素「sayHelloWorldFrom」在這個模式確實存在。我使用XPath進行搜索,這是我的代碼

InputStream isr = IOUtils.toInputStream(wsdl); 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     factory.setNamespaceAware(false); 
     try { 
      org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(isr); 
      XPathFactory xFactory = XPathFactory.newInstance(); 
      XPath xPath = xFactory.newXPath(); 
      org.w3c.dom.Element element = (org.w3c.dom.Element) xPath.compile(
         "definitions/portType/operation[@name='" + funcName + 
         "']").evaluate(doc, XPathConstants.NODE); 
      log.info("element " + element); 
      if (element == null) { 
       throw new ServiceException(ErrorCode.SOAP_EXCEPTION_005); 
      } 

但元素== NULL出於某種原因片斷,但我通過調試那是了funcName真的sayHelloWorldFrom看到。那麼問題是什麼?

回答

0

我不知道爲什麼,但是當我刪除了這部分:

xmlns="http://schemas.xmlsoap.org/wsdl/" 

XPath的工作(也許是因爲的xmlns用於XML或XPath的東西)

definitions/portType/operation[@name='sayHelloWorldFrom'] 

您可以點擊此處查看http://www.xpathtester.com/obj/2403e210-fac4-4dd2-be63-69fcba8e0ac1

+0

謝謝,但是如果我刪除這一行,我的wsdl將會失效,那麼如何在不刪除此行的情況下使此XPath正常工作? – user2390742

+0

也許你可以改變爲xmlns:wsdl(只是猜測,因爲我不擅長) – Darka

+0

正如我在這裏找到http://www.w3schools.com/xml/xml_namespaces.asp你可以改變它的名稱。 – Darka