PHP-从XML提要获取数据
我有以下代码:PHP-从XML提要获取数据,php,xml-parsing,Php,Xml Parsing,我有以下代码: <?php $url = 'http://www.floatrates.com/daily/gbp.xml'; $xml = simplexml_load_file($url) or die("feed not loading"); foreach ($xml as $val) { echo "<pre>" . print_r($val) . "</pre>"; } ?> 我怎么才能只拿到美元?然后提取汇率 我试
<?php
$url = 'http://www.floatrates.com/daily/gbp.xml';
$xml = simplexml_load_file($url) or die("feed not loading");
foreach ($xml as $val) {
echo "<pre>" . print_r($val) . "</pre>";
}
?>
我怎么才能只拿到美元?然后提取汇率
我试过了
if ($val->targetCurrency == "USD") {
echo $val->exchangeRate;
}
但是现在还不行多亏了Robbie的链接:
$url = 'http://www.floatrates.com/daily/gbp.xml';
$xml = simplexml_load_file($url) or die("feed not loading");
$results = $xml->xpath('/channel/item/targetCurrency[.="USD"]/following-sibling::exchangeRate');
$exchangeRate = (float)$results[0];
您也可以使用XPath来实现这一点 有关基本用法的示例
if ($val->targetCurrency == "USD") {
echo $val->exchangeRate;
}
$url = 'http://www.floatrates.com/daily/gbp.xml';
$xml = simplexml_load_file($url) or die("feed not loading");
$results = $xml->xpath('/channel/item/targetCurrency[.="USD"]/following-sibling::exchangeRate');
$exchangeRate = (float)$results[0];