MMMMS
MMMMS

Reputation: 2201

converting String XML to SoapMessage

I am trying to convert string xml into soapMessage using below code,

 System.out.println("response:" + response.toString());
                 InputStream is = new ByteArrayInputStream(response.toString().getBytes());
                 SOAPMessage responseSoap = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, is);
                 System.out.println("body "+responseSoap.getSOAPBody());
                 System.out.println("1");
                 QName bodyName = new QName("Response");
                  SOAPBody sb = responseSoap.getSOAPBody();
                  System.out.println("2");
                  Iterator iterator = sb.getChildElements(bodyName);
                  System.out.println("entered into SoapResponse 3");
                  while (iterator.hasNext()) {
                      System.out.println("entered into SoapResponse 4");
                    SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
                    String val = bodyElement.getValue();
                    System.out.println("The Value is:" + val);
                  }

It printing,

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><Response xmlns="http://service.com/integration/"><Result xsi:type="LoginResponse"><UserId>12</UserId><TypeId>1</TypeId><Success>true</Success></Result></Response></soap:Body></soap:Envelope>

body [soap:Body: null]

why I am getting body null?

Upvotes: 0

Views: 324

Answers (1)

HariUserX
HariUserX

Reputation: 1334

The toString() implementation of SoapBody returns "["+getNodeName()+": "+getNodeValue()+"]";. This is implemented in NodeImpl.java which is an implementation of SoapBody super class Node. In your case the getNodeValue() is null.

If your requirement is to print the response, you can do responseSoap.writeTo(System.out);.

You may also want to replace

QName bodyName = new QName("Response");

with

QName bodyName = new QName("http://service.com/integration/", "Response");

Also use String val = bodyElement.getTextContent(); if it makes sense for your use case. getValue is for Text nodes.

Upvotes: 1

Related Questions