2

I am getting null when i used below code for SOAPBOAY, what is the wrong. past 3 days i am struggling. In my project i want to convert response to class object , but when i print message.getSOAPBody().toString() its coming as NULL. Please any answers , it will help full for my service automation project.

String resMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<soapenv:Envelope\r\n"
        + "        xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n"
        + "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\r\n"
        + "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" + "  <soapenv:Header>\r\n"
        + "    <ns1:RequestHeader\r\n"
        + "         soapenv:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"\r\n"
        + "         soapenv:mustUnderstand=\"0\"\r\n"
        + "         xmlns:ns1=\"https://www.google.com/apis/ads/publisher/v201705\">\r\n"
        + "      <ns1:networkCode>123456</ns1:networkCode>\r\n"
        + "      <ns1:applicationName>DfpApi-Java-2.1.0-dfp_test</ns1:applicationName>\r\n"
        + "    </ns1:RequestHeader>\r\n" + "  </soapenv:Header>\r\n" + "  <soapenv:Body>\r\n"
        + "    <getAdUnitsByStatement xmlns=\"https://www.google.com/apis/ads/publisher/v201705\">\r\n"
        + "      <filterStatement>\r\n" + "        <query>WHERE parentId IS NULL LIMIT 500</query>\r\n"
        + "      </filterStatement>\r\n" + "    </getAdUnitsByStatement>\r\n" + "  </soapenv:Body>\r\n"
        + "</soapenv:Envelope>";

// Create SoapMessage
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();

// Load the SOAP text into a stream source
byte[] buffer = resMessage.getBytes();
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
StreamSource source = new StreamSource(stream);

// Set contents of message
soapPart.setContent(source);

// -- DONE

message.writeTo(System.out);

if (message != null) {
    System.out.println("The Response Body is " + message.getSOAPBody().toString());
}
kennyzx
  • 12,845
  • 6
  • 39
  • 83
Babusr01
  • 21
  • 1
  • 3

1 Answers1

1

Take a look at this:

XML Document to String?

I used this bit of code to resolve my issue:

public static String toString(Document doc) {
  try {
    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
  } catch (Exception ex) {
    throw new RuntimeException("Error converting to String", ex);
  }
}

Seems a bit long winded but works pretty nicely and I didn't need to set output properties (left them in there just in case). It even adds an xml declaration to the output. Should easily be able to remove that if you don't want it.