0

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();
xsw.writeCData(xml)

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

When I run this code:

XmlStreamWriter xsw =new XmlStreamWriter();
xsw.writeStartElement(null, 'x', null);
xsw.writeCData('<y>abc</y>');
xsw.writeEndElement();
System.debug(xsw.getXmlString());

it outputs:

<x><![CDATA[<y>abc</y>]]></x>

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

2
  • 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 '16 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 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.