如何使用PHP解析此XML字符串

时间:2015-06-02 14:49:54

标签: php xml xml-parsing

我很难解析以下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

请帮助我解决我的错误。

谢谢,

卡尔文

1 个答案:

答案 0 :(得分:2)

该字符串包含格式不正确的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 )