Unformatted output when printing a SOAP message with StreamResult or after modfiying its DOM structure



I´d like to output the content of a some SOAPMessage (a request and a response in this case), let´s say to the standard output, and I am having issues to see the content in a well-formatted way:



// Create the Message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// In my case I get the content from a known well XML file (which is well formatted, BTW)
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(new FileInputStream(targetFile));
sp.setContent(prepMsg);

// Add some info, save changes and send to output
SOAPElement soapHeaderElem = msg.getSOAPHeader().addChildElement("header", "","xmlapi_1.0");
SOAPElement securityHeaderElem = soapHeaderElem.addChildElement("security");
SOAPElement userHeaderElem = securityHeaderElem.addChildElement("user");
userHeaderElem.setValue(user);
SOAPElement passHeaderElem = securityHeaderElem.addChildElement("password");
passHeaderElem.addAttribute(new QName("hashed"), String.valueOf(hashedPassword));
passHeaderElem.setValue(password);

msg.saveChanges();
msg.writeTo(System.out);


Here, the only part that is well formatted is the part loaded from the XML, the rest is displayed in a single line!



<?xml version="1.0" encoding="UTF-8" ?>
<SOAP:Envelope xmlns:SOAP="http://ift.tt/sVJIaE">
<SOAP:Header>
<header xmlns="xmlapi_1.0"><security><user>****</user><password hashed="false">****</password></security></header></SOAP:Header>
<SOAP:Body>
</SOAP:Body>
</SOAP:Envelope>


On the other hand, when I inspect the body of the response using a DomSourceand a StreamResult the content of the message is displayed in a single line (all of it):



SOAPMessage rp = soapConnection.call(msg, remoteURL);
// Iterate
Source sc = new DOMSource(rp.getSOAPPart().getEnvelope().getBody().getChildNodes().item(i));

// And for each...
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
StreamResult result = new StreamResult(System.out);


My questions are:



  • Is there any property I set to those objects to have a well formatted output in any of the cases? (Something similar to the JAXB Marshaller.JAXB_FORMATTED_OUTPUT property)?

  • Is there a way to force a reformat of the message being sent?

  • Do you have a better way to do this?


No comments:

Post a Comment