title: Marshal JAXB element into SOAP handling escape characters
body: <p>I am trying to Marshal the JAXB element into SOAPBody wrappers, i was able to escape the encoded characters in JAXB, but when i marshal it into SOAP document it's not escaping these xml characters.Any help.Thanks in advance.</p>
  
  <p>And My project is small we are not utilizing any external packages,trying to do this in open jdk. </p>
  
  <pre><code>         package test;
  
          import java.io.File;
          import java.io.IOException;
          import java.io.OutputStreamWriter;
          import java.io.UnsupportedEncodingException;
          import java.io.Writer;
  
          import javax.xml.bind.JAXBContext;
          import javax.xml.bind.JAXBException;
          import javax.xml.bind.Marshaller;
          import javax.xml.soap.MessageFactory;
          import javax.xml.soap.SOAPBody;
          import javax.xml.soap.SOAPEnvelope;
          import javax.xml.soap.SOAPException;
          import javax.xml.soap.SOAPMessage;
          import javax.xml.soap.SOAPPart;
  
          import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
  
          public class JAXBExample {
              public static void main(String[] args) throws SOAPException, IOException {
  
                  Customer customer = new Customer();
                  customer.setId(100);
                  customer.setName("Hel..&lt;..lo");
                  customer.setAge(29);
  
                  try {
  
                      MessageFactory messageFactory = MessageFactory.newInstance();
                      SOAPMessage soapMessage = messageFactory.createMessage();
                      SOAPPart soapPart = soapMessage.getSOAPPart();
                      SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
                      SOAPBody soapBody = soapEnvelope.getBody();
                      JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
                      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                      // jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
                      jaxbMarshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler",
                              new CharacterEscapeHandler() {
                                  public void escape(char[] ch, int start, int length, boolean isAttVal, Writer writer)
                                          throws IOException {
                                      writer.write(ch, start, length);
                                  }
                              });
  
  
                      // output pretty printed
                      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                      jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  
                      jaxbMarshaller.marshal(customer, System.out);
                      jaxbMarshaller.marshal(customer, soapBody);
                      soapMessage.saveChanges();
                      soapMessage.writeTo(System.out);
  
                  }
                  catch (JAXBException e) {
                      e.printStackTrace();
                  }
  
              }
          }
  
  
   // result just with jaxb ***
      &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
      &lt;customer id="100"&gt;
          &lt;payload&gt;&lt;![CDATA[Hel..&lt;..lo]]&gt;&lt;/payload&gt;
          &lt;age&gt;29&lt;/age&gt;
          &lt;name&gt;Hel..&lt;..lo&lt;/name&gt;
      &lt;/customer&gt;
  // result jaxb wrapped into SOAPbody***
      &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;SOAP-ENV:Header/&gt;
      &lt;SOAP-ENV:Body&gt;
      &lt;customer id="100"&gt;
      &lt;payload&gt;&amp;lt;![CDATA[Hel..&amp;lt;..lo]]&amp;gt;&lt;/payload&gt;&lt;age&gt;29&lt;/age&gt;&lt;name&gt;Hel..&amp;lt;..lo&lt;/name&gt;&lt;/customer&gt;
      &lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;
  </code></pre>