Java SOAP - Need Help Manipulating Body and ChildElement

I am trying to write some code in java to learn more about coding using WSDL and SOAP.

For instance:

'<'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 is as follows:

 //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 

I run this and I get nothing in return, just empty. I know that my endpoint is correct, as well as checkAccount, id and password, since I tested it in xmlSpy and it returns the status of the account.

This should be the way I'm trying to get an answer. Can someone please give me a hint?

+4
source share
1 answer

This is how I 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()); 

When you use the namespace 'to = http: // foo ' for the first time, it is automatically declared in the element - checkAccount in this case. When you use the same namespace again, XML will not need to be declared again, but will use a prefix.

The result is as follows:

 <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> 

This is what you want, I think.

+3
source

Source: https://habr.com/ru/post/1313043/


All Articles