I have a SOAPMessage
(found in javax.xml.soap.SOAPMessage
).
However the only way to print it seems to be soapMessage.writeTo(System.out);
However this doesn't have any new lines and with a large SOAPMessage
it can be difficult to read.
Further, using System.out.println(soapMessage.toString());
just prints out:
com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl@76c7e77a
I looked at How to pretty print XML from Java?, How to print SOAPMessage and How to convert SOAPBody to String but neither fix the issue of newlines and/or formatting a SOAPMessage
.
If you don't mind adding additional dependencies to your project then jdom provides good formatted output for XML.
The documentation at jdom.org is well worth a look.
It's a little convoluted but you can write the XML in to a JDOM Document object and then use the XMLOutputter object to print it in a pretty format.:
// write the SoapMessage to a String called xml
File file= new File(pathToFile);
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
soapMessage.writeTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
SAXBuilder b = new SAXBuilder();
Document doc = b.build(file);
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
xmlOutputter.output(doc, System.out);