JavaSOAP-需要有关Body和ChildElement操作的帮助吗

JavaSOAP-需要有关Body和ChildElement操作的帮助吗,java,soap,wsdl,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="checkAccountRespon

我正在尝试用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>
我运行这个,我没有得到任何回报,只是空白。我知道我的端点以及checkAccount、id和密码都是正确的,因为我在xmlSpy中测试了它,它返回帐户状态


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

我会这样做的

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());
在本例中,第一次使用名称空间“to=”时,它会在元素-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>

测试
测试

这就是你想要的我想

谢谢你的帮助Dunderklumpen,我更改了代码,使它看起来像你的,因为它更容易阅读。但问题似乎不是这样,而是我需要如何阅读和处理回复。我更新了我的帖子,所以你能看看我哪里错了吗?另外,我正在使用eclipse,您如何知道您的输出是什么样子的呢?我添加了一些代码来演示如何打印SOAP消息。当然,这不会格式化XML(它全部写在一行上)。您可以通过执行以下操作使用eclipse对其进行格式化。1.从out控制台2复制XML。在eclipse中创建一个新的文本文档粘贴内容3。将文档另存为.xml 4。重新打开文档并选择格式(ctrl+shift+f)。对于小型XML字符串来说,这并不值得付出努力,但是对于大型XML字符串来说,这是非常有用的。如果您想看到SOAP请求来来往往,就应该考虑使用TCPMon。可以扮演“中间人”的角色。您可以将TCPMon配置为侦听您的SOAP请求并将其发送到适当的服务器,然后它将响应传递回您。这是查看请求和响应的一种有效方法。多亏了您,现在我可以看到错误是什么了:
xml.apache.org/axis/“>ns1:Client.NoSOAPAction no SOAPAction header!xml.apache.org/axis/”>foo1
这是否意味着我需要给它一个头?
<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>