我有一个通过桌面应用程序通过POST收到的非常简单的xml:
<?xml version="1.0" encoding="utf-8"?>
<root>
<receipt status="1" id="PAR/2" idreceipt="1" date="YYMMDD" errorstr="" />
<receipt status="1" id="PAR/2/2" idreceipt="2" date="YYYY-MM-DD HH:II:SS" errorstr="" />
<receipt status="0" id="PAR/2/3" idreceipt="3" date="YYYY-MM-DD HH:II:SS" errorstr="ERROR" />
</root>
我将其保存到变量,然后尝试使用将其加载到数组simplexml_load_string($string)
。var_dump($xml)
返回false。我知道没有内容,但是当我尝试在每张收据上print_r
使用属性时foreach
,它也是空的。xml格式不正确,还是我在PHP中缺少某些内容?
整个PHP:
$string = $_POST['results'];
$xml = simplexml_load_string($string);
foreach ($xml->receipt as $value) {
print_r($value);
}
因此,我已经尝试了几乎所有内容-使用Ako提供的解决方案重新安装该模块,但没有成功。事实证明,我的服务器在处理POST时将每个特殊字符转换为字符实体,并且解析器无法使用<
和>
而不是实际将字符串识别为有效的XML,<
>
因此我添加了:
$string = html_entity_decode($string);
而且效果很好。希望这对其他人有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句