I'm trying to write some code in java to learn more about coding with WSDL and SOAP.
For example given :
'<'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>
my code looks like this at the moment:
//create the message
String endpoint = "http://foo/someAPI";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage m开发者_Go百科essage = 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
I run this and i get nothing in return , just blank. I know my endpoint is correct , as well as the checkAccount ,id and password since I've tested it in xmlSpy and it returns account status.
It has to be the way I'm trying to get the response. Can someone please give me a hint?
This is how I would do it.
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());
The first time you use the namespace 'to=http://foo' it is automatically declared on the element - checkAccount in this case. When you use the same namespace again, the XML won't need to declare it again, but will use the prefix.
The output looks like:
<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>
Which is what you want I think
精彩评论