Java SOAP - 需要有关 Body 和 ChildElement 操作的帮助

ke3*_*pup 5 java soap wsdl

我正在尝试用 java 编写一些代码,以了解有关使用 WSDL 和 SOAP 进行编码的更多信息。

例如给出:

'<'to:checkAccount xmlns:to="http://foo">
       '<'to:id>  test  '<'/to:id>
       '<'to:password>  test  '<'/to:password>
'<'to:checkAccount >"

'<'element name="checkAccountResponse"> '<'complexType> '<'sequence> '<'element name="checkAccountReturn" type="impl:account"/> '<'/sequence> '<'/complexType> '<'/element>

'<'complexType name="account"> '<'sequence> '<'element name="active" type="xsd:boolean"/> '<'element name="name" type="xsd:string"/> '<'/sequence> '<'/complexType>

我的代码现在看起来像这样:


//create the message
            String endpoint = "http://foo/someAPI";

            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();


            SOAPPart soapPart = message.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPHeader header = message.getSOAPHeader();

            //adding to the body
            SOAPBody body = message.getSOAPBody();
            SOAPFactory soapFactory = SOAPFactory.newInstance();
            Name bodyName = soapFactory.createName("checkAccount","to","http://foo");
            SOAPElement bodyElement = body.addBodyElement(bodyName);

            //add the ID child elements
            soapFactory = SOAPFactory.newInstance();
            Name childName = soapFactory.createName("id","to","http://foo");
            SOAPElement symbol = bodyElement.addChildElement(childName);
            symbol.addTextNode("test");

            //add password child element
            soapFactory = SOAPFactory.newInstance();
            childName = soapFactory.createName("password","to","http://foo");
            symbol = bodyElement.addChildElement(childName);
            symbol.addTextNode("test");


            //call and get the response
            SOAPMessage response = sc.call(message,endpoint);


            //print the response
            SOAPBody responseBody = response.getSOAPBody();
            java.util.Iterator iterator = responseBody.getChildElements(bodyName);
.
.
.
//the response is blank so trying to iterate through it gives the exception
Run Code Online (Sandbox Code Playgroud)

我运行这个,但没有得到任何回报,只是一片空白。我知道我的端点以及 checkAccount、id 和密码是正确的,因为我已经在 xmlSpy 中对其进行了测试,并且它返回了帐户状态。

这一定是我试图得到回应的方式。有人可以给我提示吗?

Dun*_*pen 5

我就是这样做的。

MessageFactory factory = MessageFactory.newInstance();           
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
SOAPElement checkAccEl =  body
  .addChildElement("checkAccount", "to", "http://foo");

SOAPElement idEl = checkAccEl
  .addChildElement("id", "to", "http://foo");
idEl.addTextNode("test");

SOAPElement passwordEl = checkAccEl
  .addChildElement("password", "to", "http://foo");
passwordEl.addTextNode("test");

// print out the SOAP Message. How easy is this?!
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
System.out.println(out.toString());
Run Code Online (Sandbox Code Playgroud)

第一次使用命名空间 'to= http://foo ' 时,它会自动在元素上声明 - 在本例中为 checkAccount。当您再次使用相同的命名空间时,XML 不需要再次声明它,但会使用前缀。

输出看起来像:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <to:checkAccount xmlns:to="http://foo">
            <to:id>test</to:id>
            <to:password>test</to:password>
         </to:checkAccount>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

我想这就是你想要的