Preserve white spaces in a SOAP message
Posted by Vivek on January 30, 2009
Sometimes, the business requirement mandates that a SOAP request/response should maintain leading or trailing spaces that are present in a variable. By default, this does not happen. For example, you may expect ” ABC” or “ABC “, but you get “ABC”. The spaces are removed. Also, you may encounter issues if you use a binding mechanism like JAXB to marshal/ unmarshal xml fragments. This requires modifying the default marshalling or unmarshalling behavior. This is how you can still maintain any whitespaces:
Before marshalling call
which informs MarshallerImpl#createWriter(…) to create a UTF8XmlOutput instead of SAXOutput.
UTF8XmlOutput escapes line-break into “ ”, which will be preserved when unmarshalling is performed by any xml parser.