2015-06-02 42 views
1

解析以下XML字符串時遇到困難。 當使用SimpleXMLElementsimplexml_load_string時,我收到許多錯誤。 我正在運行PHP版本5.5.20。如何使用PHP解析此XML字符串

<?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 

請幫我undertand我在做什麼錯。

感謝,

卡爾文

+0

那麼,它不會出現良好的。 標籤在哪裏? –

+0

我同意,它不是很好形成。這是來自外部的迴應,不幸的是我無法控制他們發回給我的信息。然而,我可以使用一些PHP字符串操作來獲得正確的格式。我有些人會稱之爲XML newb,所以如果有人能告訴我表單應該如何,我可以在我的代碼中進行這些更改。在附註中,我在我的帖子前添加了''標籤,並且我沒有注意到上面顯示的錯誤有任何更改,所以我不認爲這是問題所在。 – Calvin

+0

你沒有做錯任何事情,這是你如何從一個字符串中創建'SimpleXMLElement'對象。然而,字符串不好轉換爲XML對象,你應該清理它,並仔細閱讀返回的錯誤,他們是非常簡單的 – smarber

回答

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) 
+0

這是偉大的,它的工作原理,謝謝。如果我有名譽,我會高興。 – Calvin

+0

我有一個跟進問題給你。你能告訴我如何訪問''的屬性值(我需要獲得「NAK」)嗎? – Calvin

+0

http://php.net/manual/en/simplexmlelement.attributes.php#refsect1-simplexmlelement.attributes-examples – smarber