PHP simpleXML 解析 [英] PHP simpleXML parsing

查看:53
本文介绍了PHP simpleXML 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要货币兑换,欧元到美元.
欧洲中央银行在此处提供利率:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我可以通过使用第一个节点获得美元汇率,但如果他们更改订单怎么办?
我需要更可靠的东西吗?我不知道怎么...

I need currency conversion, euro to dollar.
The European Central bank provides the rates here:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
I can get the USD rate by using the first node, but what if they change the order?
Do I need something more reliable? I have no idea how..

$xml = @simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
echo "dollar: " . $xml->Cube->Cube->Cube[0]->attributes()->rate;

推荐答案

只需使用 XPath 获取属性 @currency 等于USD"的任何节点,即可解决问题.

Just use XPath to get any node with the attribute @currency equal to "USD", that will do the trick.

$xref  = simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$nodes = $xref->xpath('//*[@currency="USD"]');

echo $nodes[0]['rate'];

这篇关于PHP simpleXML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆