File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes SOAPMessage.writeTo() - No line breaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "SOAPMessage.writeTo() - No line breaks " Watch "SOAPMessage.writeTo() - No line breaks " New topic
Author

SOAPMessage.writeTo() - No line breaks

Eddy McGregor
Greenhorn

Joined: Sep 20, 2005
Posts: 3
Hi All

In the SAAJ API there is the SOAPMessage.writeTo(java.io.OutputStream). This writes out the XML of the SOAP message, BUT with NO line breaks.

Does anyone know of / have a convenient method WHICH INCLUDES line braeks
and indents - so you see the XML in it's normal form ??

Cheers Eddy
Eddy McGregor
Greenhorn

Joined: Sep 20, 2005
Posts: 3
Forgot to add
Or if there is some configuration that can be done ....

Cheers Eddy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
You could capture the OutputStream, and run it through an XML pretty-printer, such as the ones that are part of Xerces or XOM.


Ping & DNS - my free Android networking tools app
Eddy McGregor
Greenhorn

Joined: Sep 20, 2005
Posts: 3
Ok thanks Ulf, I'll check that out, Eddy
Glittery Lally
Greenhorn

Joined: Apr 08, 2010
Posts: 6
Hi guys, having the same problem.
Is there any standard solution now for this (noticed the topic is from 2005...)?
I need to save the content of the SoapMessage simply as String, not some other fancy format.

But it has to match EXACTLY the content of the soap response (also indents and line breaks)
because it contains a digital signature and the validation of the signed document fails if some parts are edited


Thanks in advance for any help!!! It's kind of urgent

Laura


"Fatti non foste a viver come bruti, ma a seguir virtute e conoscenza"
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Glittery Lally wrote:But it has to match EXACTLY the content of the soap response (also indents and line breaks)
because it contains a digital signature and the validation of the signed document fails if some parts are edited

Before the calculation of any digital signature of XML data, the data must be canonicalized, that is the XML data must be transformed to a standardized form.
References:
http://java.sun.com/developer/technicalArticles/xml/dig_signatures/
http://www.w3.org/TR/xml-c14n

Regarding your question:
You can use a XSLT transform to write XML to a file, below follows an example:

For more examples, see page 306 and forward in this document:
http://www.slideshare.net/krizsan/scdjws-5-study-notes-3085287
Best wishes!
Glittery Lally
Greenhorn

Joined: Apr 08, 2010
Posts: 6
Hi thanks for your prompt answer
The problem is not how to write the stuff in an xml file
the problem is how to take the content of a SOAPMessage object and put it into a string
when I do soapMessage.writeTo(myByteArrayOutputStream)
the byteArrayOutputStream doesn't contain the same white spaces and line breaks as the original one.
I'm gonna read the documents about the canonicalization you sent me now, thanks

R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
@Ivan, Thank you for that very helpful code example.

@Glittery Lally: After some searching based on Ivan's example, I found another link that may have exactly what you need: SOAP message handler for logging inbound SOAP results. I just tried it and it worked fine for me. Its the same as Ivan's example, except that it reads the XML from the soap message instead of from a file. Hope that helps.
Glittery Lally
Greenhorn

Joined: Apr 08, 2010
Posts: 6
@R Srini: Thanks a lot for your message and your time.
I tried your code and it works but unfortunately I have the same problem.
Some line breaks are lost
So I have to try another solution.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
I used : http://javakafunda.blogspot.in/2012/04/how-to-format-xml-string-in-java.html

and it worked for me...
 
Don't get me started about those stupid light bulbs.
 
subject: SOAPMessage.writeTo() - No line breaks
 
Similar Threads
XML question
Say 4b line breaks
character method of ContentHandler
unable to remove spaces in xml
Program: Change Due/Tendered