PHP:如何修复错误无法解析XML数据? [重复]

This question already has an answer here:

Whenever I get an & (ampersand) in my company name , it throws error Unable to parse XML data

XML data:

$xmlData = '<Leads>
<row no="1">
<FL val="Company"><![CDATA[ .$company. ]]></FL>
</row>
</Leads>';

$company = isset($company) && !empty($company) ? str_replace('&', '&amp;', $company) : 'None';

I have tried replacing & with &amp; , %26 etc and used CDATA as well, but nothing is working.

XML seems valid still it throws the error

<Leads><row no="1"><FL val="Company">test &amp; test</FL></row></Leads>

With CDATA:

<Leads><row no="1"><FL val="Company"><![CDATA[test & test]]></FL></row></Leads>

What could be the solution for it?

P.S: I am sending this xml data to zoho CRM. Zoho not accepting company name with & in it.

UPDATE 1:

This is the URL that sends xml data to zoho and when it gets & it does not work.

https://crm.zoho.com/crm/private/xml/Leads/insertRecords?newFormat=1&authtoken=authtoken&xmlData=<Leads><row no="1"><FL val="Company">test &amp; test</FL></row></Leads>

OR

https://crm.zoho.com/crm/private/xml/Leads/insertRecords?newFormat=1&authtoken=authtoken&xmlData=<Leads><row no="1"><FL val="Company">test & test</FL></row></Leads>

With &amp; it should work but it doesn't. :(

</div>

If you send this as a GET request you will need to escape the & in the url as well. In general it is a good idea to use matching APIs/functions specific for a format and not just strings.

$document = new DOMDocument();
$row = $document
  ->appendChild($document->createElement('Leads'))
  ->appendChild($document->createElement('row'));
$row->setAttribute('no', '1');
$FL = $row->appendChild($document->createElement('FL'));
$FL->setAttribute('val', 'Company');
$FL->appendChild($document->createTextNode('Foo & Bar'));

$xml = $document->saveXml($document->documentElement);
var_dump($xml);    

$url = 'https://crm.zoho.com/crm/private/xml/Leads/insertRecords?'.
  http_build_query(
    [
      'newFormat' => 1,
      'authtoken' => 'abc1234',
      'xmlData' => $xml
    ]
  );

echo $url;

Output:

string(69) "<Leads><row no="1"><FL val="Company">Foo &amp; Bar</FL></row></Leads>"
https://crm.zoho.com/crm/private/xml/Leads/insertRecords?newFormat=1&authtoken=abc1234&xmlData=%3CLeads%3E%3Crow+no%3D%221%22%3E%3CFL+val%3D%22Company%22%3EFoo+%26amp%3B+Bar%3C%2FFL%3E%3C%2Frow%3E%3C%2FLeads%3E

Of course it could be possible that the ZOHO CRM just does not accept company names with an & in it.