2010-07-27 52 views
0

閱讀SOAP mesage我有以下的SOAP消息:的Java使用XPath

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><soap:Header><wsse:Security env:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:Username>test</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">11111111</wsse:Password></wsse:UsernameToken></wsse:Security></soap:Header> 
    <soap:Body xmlns:ns1="http://www.acme.co.za"> 
     <ns1:pingMeCallback> 
      <ns1:message>abc</ns1:message> 
     </ns1:pingMeCallback> 
    </soap:Body> 
</soap:Envelope> 

我需要使用XPath的消息值讀取。我的代碼如下:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 

    InputSource data = new InputSource(in); 
    Node doc = builder.parse("ping.xml"); 

    // set up a document purely to hold the namespace mappings 
    DOMImplementation impl = builder.getDOMImplementation(); 
    Document namespaceHolder = impl.createDocument("http://www.acme.co.za/","f:namespaceMapping", null); 
    Element root = namespaceHolder.getDocumentElement(); 

    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:SOAP","http://schemas.xmlsoap.org/soap/envelope/"); 
    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1","http://schemas.xmlsoap.org/soap/envelope/"); 

    NodeList results = XPathAPI.selectNodeList(doc,"/SOAP:Envelope/SOAP:Body/ns1:pingMeCallback/ns1:message",root); 

    for (int i = 0; i < results.getLength(); i++) { 
    Node result = results.item(i); 
    XObject value = XPathAPI.eval(result, "string()"); 
    System.out.println(value.str()); 
    } 

我的代碼不打印任何東西。任何人都可以知道我在做什麼?

回答

0

有沒有通過代碼的其餘部分了,但:

root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1","http://schemas.xmlsoap.org/soap/envelope/"); 

看起來並不像正確的命名空間。

+0

我已將其更改爲:root.setAttributeNS(「http://www.w3.org/2000/xmlns/」,「xmlns:ns1」,「hhttp://www.acme.co.za」) ;仍然沒有運氣 – teslaza 2010-07-27 12:15:57

+0

仍然不太匹配。雙'h';還檢查尾隨與非尾隨'/'。命名空間是必須完全匹配的字符串。 – bobince 2010-07-27 14:05:08