4 Replies Latest reply on Aug 23, 2010 5:30 AM by jcs_jean-claude.souvignet

    CXF - activate XML_DECLARATION from soap exchange

    jcs_jean-claude.souvignet

      Hello,

       

      I use CXF 2.2.7 to declare one service.

       

      Service client isn't CXf nor java. It needs XML declaration :" <?xml version="1.0" encoding="UTF-8"?> ".

       

      I test to set up SOAPMessage.WRITE_XML_DECLARATION to true in process to force CXF to write the line.

      But the line is not written.

       

       

      I declare an handler in jaxws cxf service declaration :

      -


       

      -


       

      The handler is the attached file.

       

      Do you have a solution to write this XML declaration  ? By this way or by another ?

      Thanks in advance for your help.

        • 1. Re: CXF - activate XML_DECLARATION from soap exchange
          jcs_jean-claude.souvignet

          Hello,

           

          more detail on my need :

           

          Client side, the XML result from my web service need the xml definition in the beginning of the message :

          <?xml version="1.0" encoding="XXXXXXX"?>

          <soap:Envelope ....

           

          By default, my message begin with : <soap:Envelope ....

           

           

          I don't know if it is possible to do so by CXF configuration. Does anybody have had the same need ?

          Should I create my own interceptor and activate it in WRITE phase ?

           

          thanks in advance for your help.

           

          Edited by: jcs on May 27, 2010 2:18 PM

          • 2. Re: CXF - activate XML_DECLARATION from soap exchange
            njiang

            Hi,

            Because CXF write the XML message without using the SOAPMessage write method to write the message, so setting the SOAPMessage.WRITE_XML_DECLARATION to be true doesn't help you.

             

            Here is CXF interceptor that did the trick, it will tell StaxOutInterceptor to write the xml start document for you.

            public class WriteXmlDeclarationInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
                public WriteXmlDeclarationInterceptor() {
                    super(Phase.PRE_STREAM);
                    addBefore(StaxOutInterceptor.class.getName());
                }
            
                public void handleMessage(SoapMessage message) throws Fault {
                    message.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE);        
                }
            
            }
            

             

            Willem

            • 3. Term Papers
              albertwilson

              I'm very thankful to the author for posting such an amazing development post. Continuing to the post. Thanks.

               

              Term Papers

              • 4. Re: CXF - activate XML_DECLARATION from soap exchange
                jcs_jean-claude.souvignet

                Thanks to Willen Njiang.          

                WriteXmlDeclarationInterceptor do the stuff.