我從http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml如何通過屬性選擇xml元素而無需迭代使用PHP?
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2013-08-23'>
<Cube currency='USD' rate='1.3355'/>
<Cube currency='GBP' rate='0.85910'/>
<Cube currency='HUF' rate='298.98'/>
</Cube>
</Cube>
</gesmes:Envelope>
這個XML(我刪除示範目的的一些值),我想要得到的轉換率,爲讓說,GBP使用PHP
。
HI可以使用SimpleXML這樣加載:
$XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
foreach($XML->Cube->Cube->Cube as $rate){
...
但我想獲得價值不反覆,我真的不想使用正則表達式...
我想是這樣這一點,但它沒有工作:
$sxe=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
$sxe->registerXPathNamespace('c', 'http://www.gesmes.org/xml/2002-08-01');
$result = $sxe->xpath('//c:Cube[@currency="USD"]');
*沒有工作*在哪種方式,完全是? – michi
我剛收到一個空數組 –
也許是因爲XML中沒有'RUB'? – michi