【发布时间】:2015-06-02 14:49:53
【问题描述】:
我在解析以下 XML 字符串时遇到了困难。
使用SimpleXMLElement 或simplexml_load_string 时收到许多错误。
我正在运行 PHP 版本 5.5.20。
<?xmlversion="1.0"encoding="utf-8"?>
<Responsetype="NAK">
<ResponseCode>231</ResponseCode>
<Description>Billingstate/provinceisrequired.</Description>
<Reference>VSTMUAS:060215CJM-12</Reference>
<TransactionID>1433251975406510979</TransactionID>
<ProcessingTime>0.590634</ProcessingTime>
</Response>
当我运行以下代码时:
$xml = simplexml_load_string($myXMLData);
print_r($xml);
我收到以下错误:
Warning: simplexml_load_string(): Entity: line 1: parser warning : xmlParsePITarget: invalid name prefix 'xml' in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <?xmlversion="1.0"encoding="utf-8"?> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): Entity: line 1: parser error : ParsePI: PI xmlversion space expected in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <?xmlversion="1.0"encoding="utf-8"?> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): Entity: line 2: parser error : error parsing attribute name in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): Entity: line 2: parser error : attributes construct error in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): Entity: line 2: parser error : Couldn't find end of Start Tag Responsetype line 2 in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): Entity: line 2: parser error : Extra content at the end of the document in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18
请帮助我理解我做错了什么。
谢谢,
卡尔文
【问题讨论】:
-
嗯,它看起来格式不正确。打开
标记在哪里? -
我同意,它的格式不正确。这是来自外部来源的回应,不幸的是我无法控制他们发回给我的内容。但是,我可以使用一些 php 字符串操作来以正确的形式获取它。我是一些人所说的 XML 新手,所以如果有人可以告诉我表单应该如何,我可以在我的代码中进行这些更改。附带说明一下,我确实在我的帖子之前添加了
<Response>标签,我没有注意到上面显示的错误有任何变化,所以我认为这不是问题。 -
您没有做错任何事情,这就是您从字符串创建
SimpleXMLElement对象的方式。但是字符串不适合转换为 XML 对象,您应该清理它并仔细阅读返回的错误,它们非常简单
标签: php xml xml-parsing