You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by "David L. Brown" <db...@cox.net> on 2006/02/13 17:51:26 UTC

Controlling serialization

Is there a way to control the XML serialization of a SOAPMessage?  I'm 
using Axis for Java 1.2.

If I input a SOAP message that is all on one line, parse it, add a 
header, then serialize it, the message has new whitespace in it (tabs 
and carriage returns).  How can I stop it from generating this new 
whitespace?

For example for the code that follows ...
    private static void runTest1 () throws Exception
    {
        System.out.println ("Test #1 ...\n");

        SOAPMessage message = loadMessageText (messageText1);
        SOAPHeader header = message.getSOAPHeader();
        SOAPHeaderElement headerElement = new SOAPHeaderElement 
("http://localhost", "testy");
        headerElement.setActor(null);
        header.addChildElement(headerElement);

        message.writeTo (System.out);

        System.out.println ("\n");
    }

    private static SOAPMessage loadMessageText (String messageText)
            throws Exception
    {
        MessageFactory messageFactory = MessageFactory.newInstance ();
        SOAPMessage message = messageFactory.createMessage (new 
MimeHeaders (),
                new ByteArrayInputStream (messageText.getBytes ()));
        return message;
    }

where messageText1 equals the following ...

<?xml version="1.0" encoding="UTF-8"?><SOAP:Envelope 
xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><echo 
xmlns="http://localhost:8080/test/services/Text"><in0 xmlns="">This is 
the message to echo</in0></echo></SOAP-ENV:Body></SOAP:Envelope>

My output is

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
 <SOAP:Header>
  <ns1:testy SOAP:mustUnderstand="0" xmlns:ns1="http://localhost"/>
 </SOAP:Header>
 <SOAP:Body>
  <ns2:echo xmlns:ns2="http://localhost:8080/test/services/Text">
   <in0 xmlns="">This is a clean word:  unclassified</in0>
  </ns2:echo>
 </SOAP:Body>
</SOAP:Envelope>