Java SOAP - 在Body和ChildElement操作方面需要帮助
问题描述:
我想在java中编写一些代码,以了解有关使用WSDL和SOAP编码的更多信息。Java SOAP - 在Body和ChildElement操作方面需要帮助
例如,给定:
'<'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
我运行此,我也得到任何回报,只是空白。我知道我的端点是正确的,以及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());
您使用的命名空间“为= 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>
这是你想要什么,我认为
感谢您的帮助Dunderklumpen,我改变了我的代码,以便它看起来像你的自更容易阅读。但看起来问题不是这样,更像是我需要阅读和处理回复。我更新了我的帖子,所以请你看看我的错在哪里? 我也使用eclipse,你怎么知道你的输出看起来如何? – ke3pup 2010-06-17 06:17:40
我已经添加了一些代码来向您展示如何打印出SOAP消息。当然这不会格式化XML(它全部写在一行上)。通过执行以下操作,可以使用eclipse格式化它。 1.从输出控制台复制XML 2.在eclipse中创建一个新的文本文档粘贴内容 3.保存文档为 .xml 4.重新打开文档并选择格式(ctrl + shift + f)。 对于小型XML字符串,这并不值得付出努力,但对于大型XML字符串,它可能非常有用。 –
Dunderklumpen
2010-06-17 06:32:00
如果您想查看来来去去的SOAP请求,您应该考虑使用TCPMon。 https://tcpmon.dev.java.net/可以充当“中间人”。您可以将TCPMon配置为侦听您的SOAP请求并将其发送到适当的服务器,然后将响应传递给您。它是查看请求和响应的有效方式。 – Dunderklumpen 2010-06-17 06:36:24