+ Start a Discussion
Sankalp JhingranSankalp Jhingran 

Less than sign(<) is converting to &lt;(HTML) in the SOAP request generated by SFDC

Hi All,

I have generated an apex class from a WSDL and I am making callouts to a third party webservice. It has a edit method which accepts 3 arguments, 1st username, 2nd password and 3rd accepts an XML within CDATA tags.

Callout is working fine when the request is generated using SOAP UI, however from salesforce all less than signs(<) in the 3rd parameter are getting converted to "&lt;" in the final request.

Here is the line I am passing for the 3rd parameter in CDATA tag:
String xml = '<Contract id="Contract-6765031"><ContractName>1000 CRM Subscriptions2</ContractName><ContractId>2320</ContractId><Division>6749812</Division><SubDivision>6749934</SubDivision><Vendor>6749092</Vendor><Description>1000 CRM Subscriptions2</Description></Contract>';

XmlStreamWriter xsw = new XmlStreamWriter();
String xmlString = xsw.getXmlString();

Here is the resulting string printed with CDATA tags:
00:04:24.047 (47225666)|USER_DEBUG|[73]|DEBUG|############## <![CDATA[<Contract id="Contract-6765031"><ContractName>1000 CRM Subscriptions2</ContractName><ContractId>2320</ContractId><Division>6749812</Division><SubDivision>6749934</SubDivision><Vendor>6749092</Vendor><Description>1000 CRM Subscriptions2</Description></Contract>]]>

Here is the debug for the request object:
00:16:20.067 (67571169)|USER_DEBUG|[312]|DEBUG|#######################1 edit_element:[apex_schema_type_info=(http://xmlns.enlighta.com/WebService, true, false), field_order_type_info=(in0, in1, in2), in0=sankalp@someemail.com, in0_type_info=(in0, http://xmlns.enlighta.com/WebService, null, 1, 1, false), in1=password, in1_type_info=(in1, http://xmlns.enlighta.com/WebService, null, 1, 1, false), in2=<![CDATA[<Contract id="Contract-6765031"><ContractName>1000 CRM Subscriptions2</ContractName><ContractId>2320</ContractId><Division>6749812</Division><SubDivision>6749934</SubDivision><Vendor>6749092</Vendor><Description>1000 CRM Subscriptions2</Description></Contract>]]>, in2_type_info=(in2, http://xmlns.enlighta.com/WebService, null, 1, 1, false)]

Now, when I saw the complete SOAP request in the debug log, I found &lt; signs in the request for the 3rd parameter as below:
00:16:20.071 (71082584)|CALLOUT_REQUEST|[314]|<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Header /><env:Body><edit xmlns="http://xmlns.enlighta.com/WebService"><in0>sankalp@someemail.com</in0><in1>password</in1><in2>&lt;![CDATA[&lt;Contract id="Contract-6765031">&lt;ContractName>1000 CRM Subscriptions2&lt;/ContractName>&lt;ContractId>2320&lt;/ContractId>&lt;Division>6749812&lt;/Division>&lt;SubDivision>6749934&lt;/SubDivision>&lt;Vendor>6749092&lt;/Vendor>&lt;Description>1000 CRM Subscriptions2&lt;/Description>&lt;/Contract>]]&gt;</in2></edit></env:Body></env:Envelope>

So, the xml reaching to the third party is incorrect.
Any help is appreciated.

Hi Sankalp, 

2.4 Character Data and Markup

Text consists of intermingled character data and markup. [Definition: Markup takes the form of start-tags, end-tags, empty-element tags, entity references, character references, comments, CDATA section delimiters, document type declarations, processing instructions, XML declarations, text declarations, and any white space that is at the top level of the document entity (that is, outside the document element and not inside any other markup).]

[Definition: All text that is not markup constitutes the character data of the document.]

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and must, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

In the content of elements, character data is any string of characters which does not contain the start-delimiter of any markup and does not include the CDATA-section-close delimiter, " ]]> ". In a CDATA section, character data is any string of characters not including the CDATA-section-close delimiter, " ]]> ".

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

Character Data

[14]    CharData    ::=    [^<&]* - ([^<&]* ']]>' [^<&]*)


Also see the below threads about the Same