繁体   English   中英

具有特殊字符的PHP XML UTF-8引发错误

[英]PHP XML UTF-8 with special characters throws errors

我在从DHL API接收UTF-8 XML文件时遇到一些问题。 只要我不发送任何像ś或ó这样的特殊字符,一切就可以正常工作,但是使用这些字符,当我尝试加载从DHL接收的XML文件时,我的应用程序崩溃并抛出错误:

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Opening and ending tag mismatch: AddressLine line 43 and Consignee 
in Entity, line: 53 in D:\xampp\htdocs\ebay\catch2.php on line 29

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Opening and ending tag mismatch: Consignee line 40 and res:ShipmentValidateResponse 
in Entity, line: 97 in D:\xampp\htdocs\ebay\catch2.php on line 29

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: Premature end of 
data in tag ShipmentValidateResponse line 1 in Entity, line: 98
in D:\xampp\htdocs\ebay\catch2.php on line 29

这是我发送的XML

<?xml version="1.0" encoding="utf-8"?>
... 
<AddressLine>address</AddressLine> 
<AddressLine>asfśó</AddressLine> 
...

我收到的:

<?xml version="1.0" encoding="UTF-8"?>
...
Lines 40 to 43:

<Consignee>
<CompanyName>Person</CompanyName>
<AddressLine>address</AddressLine>
<AddressLine>asf??ddressLine>
...

这是在第29行附近发生的情况:

$responseXml = $session->sendHttpRequest($requestXmlBody);
if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
    die('<P>Error sending request');
$responseXml = utf8_decode($responseXml);
$responseDoc = new DOMDocument('1.0', 'UTF-8');
$responseDoc->loadXML($responseXml);

E:删除utf8_decode并没有太大帮助。 只是一个新错误:

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Input is not proper UTF-8, indicate encoding !
Bytes: 0xF3 0x3C 0x2F 0x41 in Entity, line: 43 in D:\xampp\htdocs\ebay\catch2.php on line 29

E2:十六进制转储

0000-0010:  3c 3f 78 6d-6c 20 76 65-72 73 69 6f-6e 3d 22 31  <?xml.ve rsion="1
0000-0020:  2e 30 22 20-65 6e 63 6f-64 69 6e 67-3d 22 55 54  .0".enco ding="UT
0000-0030:  46 2d 38 22-3f 3e 3c 72-65 73 3a 53-68 69 70 6d  F-8"?><r es:Shipm

第43行:

0000-0960:  4c 69 6e 65-3e 0a 20 20-20 20 20 20-20 20 3c 41  Line>... ......<A
0000-0970:  64 64 72 65-73 73 4c 69-6e 65 3e 61-73 66 3f f3  ddressLi ne>asf?.
0000-0980:  3c 2f 41 64-64 72 65 73-73 4c 69 6e-65 3e 0a 20  </Addres sLine>..

不要使用utf8_decode

这就是搞砸您的编码的原因。
utf8_decode将UTF-8编码的文本转换为Latin1编码的文本。 那不是您想要或需要的。 只需按原样解析XML,无需进行编码转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM