function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sravana SudheerSravana Sudheer 

Unable to Send data to 3rd party due to invalid XML(which contains <p>, <ul> tags)

Hi All,
Got an error saying that unable to Post the data to 3rd party service due to invalid XML in one of the Field which we are mapping from salesforce which is in HML format. Tried using CDATA tags, but seems out like it didn't resolve the issue. 
Error: <Message>XML INVALID, Your XML is not valid according to Export DTD. unknown-65b0270:0: validity error : Element JobDescription was declared #PCDATA but contains non text nodes unknown-65b0270:0: validity error : No declaration for element p unknown-65b0270:0: validity error : No declaration for attribute style of element p
Tried using the below code.
JobDescription= rev.Job_Description__c;    --> Job_Description__c contains html tags like <ul>, <p>,<span>  etc.
String[] JD = JobDescription.split('<br>');
        JobDescription='';
        string JobDescriptionP='';
        for(String s : JD){
        JobDescription = JobDescription+'<![CDATA[<br>]]>' +s+' ';
            string[] JDP=JobDescription.split('<p>');
            for(string p:JDP){
                JobDescriptionP = JobDescriptionP+'<![CDATA[<p>]]>'+p+' ';
                        system.debug('jdp-----'+JobDescriptionP);
            }
            JobDescription=JobDescriptionP;
        system.debug('jd++++'+JobDescription);
        }
Best Answer chosen by Sravana Sudheer
pconpcon
That's not quite the right usage for CDATA.  What you'll want to do is HTML encode prior to sending.  You'll want to use the escapeXml method [1] on the String class
 
String jobDescription = rev.Job_Description__c;

if (jobDescription != null) {
    jobDescription = jobDescription.escapeXml();
}

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_escapeXml

All Answers

pconpcon
That's not quite the right usage for CDATA.  What you'll want to do is HTML encode prior to sending.  You'll want to use the escapeXml method [1] on the String class
 
String jobDescription = rev.Job_Description__c;

if (jobDescription != null) {
    jobDescription = jobDescription.escapeXml();
}

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_escapeXml
This was selected as the best answer
Sravana SudheerSravana Sudheer
Hi Pcon,
Thank you for your response. it works like a champ and even i tried this at the jobdescription node
<JobDescription><![CDATA['+JobDescription+']]></JobDescription>
this work in the same way like escape xml too.