【问题标题】:How do I parse this XML string with PHP如何用 PHP 解析这个 XML 字符串
【发布时间】:2015-06-02 14:49:53
【问题描述】:

我在解析以下 XML 字符串时遇到了困难。 使用SimpleXMLElementsimplexml_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 新手,所以如果有人可以告诉我表单应该如何,我可以在我的代码中进行这些更改。附带说明一下,我确实在我的帖子之前添加了&lt;Response&gt; 标签,我没有注意到上面显示的错误有任何变化,所以我认为这不是问题。
  • 您没有做错任何事情,这就是您从字符串创建SimpleXMLElement 对象的方式。但是字符串不适合转换为 XML 对象,您应该清理它并仔细阅读返回的错误,它们非常简单

标签: php xml xml-parsing


【解决方案1】:

字符串包含格式不正确的 XML。

这是修复后的 XML

<?xml version="1.0" encoding="utf-8"?>
<Response type="NAK">
    <ResponseCode>231</ResponseCode>
    <Description>Billingstate/provinceisrequired.</Description>
    <Reference>VSTMUAS:060215CJM-12</Reference>
    <TransactionID>1433251975406510979</TransactionID>
    <ProcessingTime>0.590634</ProcessingTime>
</Response>

试试这个xml,你会得到:

 SimpleXMLElement Object ( [@attributes] => Array ( [type] => NAK ) [ResponseCode] => 231 [Description] => Billingstate/provinceisrequired. [Reference] => VSTMUAS:060215CJM-12 [TransactionID] => 1433251975406510979 [ProcessingTime] => 0.590634 )

【讨论】:

  • 这很好,而且很有效,谢谢。如果我有声望,我会投票赞成。
  • 我有一个后续问题要问你。你能告诉我如何访问&lt;Response&gt;的属性值(我需要获取“NAK”)吗?
  • 想通了,得到属性:$xml['type'].
猜你喜欢
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
相关资源
最近更新 更多