2017-06-16 53 views
1

我正在使用SimpleXML來解析一個cXML文檔。我無法弄清楚如何從文檔中最深的元素訪問屬性。當我在下面的xml代碼片段中解析屬性「currency」時,它不存在。我發現這隻發生在最深的元素上。有沒有一種方法可以使用SimpleXML解析這個文檔?
如何使用SimpleXML訪問XML元素的最後一個屬性?

我試圖解析的XML文檔的示例部分。

<ItemOut name="ItemOut"> 
<ItemDetail name="ItemDetail"> 
    <UnitPrice name="UnitPrice"> 
     <Money currency="USD">12.99</Money> 
    </UnitPrice> 
</ItemDetail> 

我已經使用JSON編碼/解碼方式試圖...

$simpleXml=simplexml_load_string($xml); 
$json = json_encode($simpleXml); 
$xmlArray = json_decode($json,TRUE); 

和使用SimpleXML對象...
$xmlObject = new SimpleXMLElement($xml);

這裏的 array structure of the parsed document.

圖片

回答

1

我認爲你需要運行

<?php 
$xmlObject = new SimpleXMLElement($xml); 
$currency = $xmlObject-> ItemDetail -> UnitPrice -> Money -> attributes() -> currency; 
?> 

將讀取你的貨幣屬性,並在你的情況恢復EUR

+0

非常感謝你! –

相關問題