I have a requirement to pass XML as a string into SOAP request to an external system. I am generating an XML using DOM.Document class, but when I am sending it in SOAP request, it gets encoded with < replaced with &lt;

I tried to envelope in CDATA using:

XmlStreamWriter xsw = new XmlStreamWriter();

but with no luck. Even the < in CDATA envelope is getting encoded.

Please let me know if there is any method to pass the XML without being encoded.

1 Answer 1


When I run this code:

XmlStreamWriter xsw =new XmlStreamWriter();
xsw.writeStartElement(null, 'x', null);

it outputs:


which is what I think you are looking for, the embedded XML wrapped in CDATA.

  • Hi, Keith, that is true. When I see in my debug logs, things shows up correctly. However, only when I pass this to a service and trace the payload in RequestBin, I see all "<" getting encoded. Aug 2, 2016 at 12:45
  • @DeepSinghal Perhaps RequestBin encodes for presentation purposes? In my experience the text that you emit in an Apex HTTP request does not undergo any further encoding within Salesforce.
    – Keith C
    Aug 2, 2016 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .