簡體   English   中英

Java使用xpath讀取SOAP消息

[英]Java read in SOAP mesage using 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());
  }

我的代碼什么都不打印。 有人可以找出我在做什么嗎?

尚未完成其余代碼,但:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM