PHP / XML:获取特定节点的数据

时间:2016-02-03 15:57:24

标签: php xml simplexml

这是我的XML源:

https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

我如何获得“DKK”(丹麦克朗斯)的最新汇率?

我不想使用foreach并检查货币是否为DKK ......

我需要这样的东西:

$dkk_rate = $XML->Cube->Cube->currency('DKK')->rate;

知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

您可以直接使用DOM中的Xpath表达式获取值:

$document = new DOMDocument();
$document->loadXml($xml);

$xpath = new DOMXPath($document);
$xpath->registerNamespace('e', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');

echo $xpath->evaluate("number(//e:Cube[@currency='DKK'][last()]/@rate)");

输出:

7.4623

XML使用默认命名空间。 Cube个元素节点实际上位于命名空间http://www.ecb.int/vocabulary/2002-08-01/eurofxref中。 Xpath没有默认命名空间,因此您必须为其注册一个前缀,并在表达式中使用它。

  • Xpath获取任何Cube元素
    //e:Cube
  • 使用值currencyDKK属性过滤它们 //e:Cube[@currency='DKK']
  • 过滤最后一个节点的结果
    //e:Cube[@currency='DKK'][last()]
  • 获取它的rate属性节点 //e:Cube[@currency='DKK'][last()]/@rate
  • 将其转换为数字
    number(//e:Cube[@currency='DKK'][last()]/@rate)

最后一步仅适用于DOMXpath::evaluate()DOMXpath::query()SimpleXMLElement::xpath()只能返回节点列表/数组。因此,在SimpleXML中,您必须访问结果数组的第一个元素。

$root = new SimpleXmlElement($xml);
$root->registerXpathNamespace('e', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');

echo $root->xpath("//e:Cube[@currency='DKK'][last()]/@rate")[0];

答案 1 :(得分:0)

试试这个

$string = <<<XML
<?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='2016-02-03'>
            <Cube currency='USD' rate='1.0933'/>
            <Cube currency='JPY' rate='130.58'/>
            <Cube currency='BGN' rate='1.9558'/>
            <Cube currency='CZK' rate='27.025'/>
            <Cube currency='DKK' rate='7.4623'/>
            <Cube currency='GBP' rate='0.75330'/>
            <Cube currency='HUF' rate='310.55'/>
            <Cube currency='PLN' rate='4.3982'/>
            <Cube currency='RON' rate='4.5093'/>
            <Cube currency='SEK' rate='9.3580'/>
            <Cube currency='CHF' rate='1.1115'/>
            <Cube currency='NOK' rate='9.5138'/>
            <Cube currency='HRK' rate='7.6665'/>
            <Cube currency='RUB' rate='85.9910'/>
            <Cube currency='TRY' rate='3.2138'/>
            <Cube currency='AUD' rate='1.5484'/>
            <Cube currency='BRL' rate='4.3280'/>
            <Cube currency='CAD' rate='1.5234'/>
            <Cube currency='CNY' rate='7.1916'/>
            <Cube currency='HKD' rate='8.5266'/>
            <Cube currency='IDR' rate='15038.70'/>
            <Cube currency='ILS' rate='4.3320'/>
            <Cube currency='INR' rate='74.3900'/>
            <Cube currency='KRW' rate='1329.87'/>
            <Cube currency='MXN' rate='20.1098'/>
            <Cube currency='MYR' rate='4.6121'/>
            <Cube currency='NZD' rate='1.6560'/>
            <Cube currency='PHP' rate='52.370'/>
            <Cube currency='SGD' rate='1.5631'/>
            <Cube currency='THB' rate='39.199'/>
            <Cube currency='ZAR' rate='17.6950'/>
        </Cube>
    </Cube>
</gesmes:Envelope>  
XML;

$xml = new SimpleXMLElement($string);

$result = $xml->xpath('//*[@currency="DKK"]');

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

答案 2 :(得分:-1)

我认为你的xml数据存在一些问题,因为我试图使用你的数据格式,但似乎它不起作用,我试图删除xml中的一些条目。

这里是测试数据和代码

<?xml version="1.0" encoding="UTF-8"?>
    <Cube>
        <Cube time='2016-02-03'>
            <Cube currency='USD' rate='1.0933'/>
            <Cube currency='JPY' rate='130.58'/>
            <Cube currency='BGN' rate='1.9558'/>
            <Cube currency='CZK' rate='27.025'/>
            <Cube currency='DKK' rate='7.4623'/>
            <Cube currency='GBP' rate='0.75330'/>
            <Cube currency='HUF' rate='310.55'/>
            <Cube currency='PLN' rate='4.3982'/>
            <Cube currency='RON' rate='4.5093'/>
            <Cube currency='DKK' rate='7.2'/>
            <Cube currency='SEK' rate='9.3580'/>
            <Cube currency='CHF' rate='1.1115'/>
            <Cube currency='NOK' rate='9.5138'/>
            <Cube currency='HRK' rate='7.6665'/>
            <Cube currency='RUB' rate='85.9910'/>
            <Cube currency='TRY' rate='3.2138'/>
            <Cube currency='DKK' rate='7.1'/>
            <Cube currency='AUD' rate='1.5484'/>
            <Cube currency='BRL' rate='4.3280'/>
            <Cube currency='CAD' rate='1.5234'/>
            <Cube currency='CNY' rate='7.1916'/>
            <Cube currency='HKD' rate='8.5266'/>
            <Cube currency='IDR' rate='15038.70'/>
            <Cube currency='ILS' rate='4.3320'/>
            <Cube currency='INR' rate='74.3900'/>
            <Cube currency='KRW' rate='1329.87'/>
            <Cube currency='MXN' rate='20.1098'/>
            <Cube currency='MYR' rate='4.6121'/>
            <Cube currency='NZD' rate='1.6560'/>
            <Cube currency='PHP' rate='52.370'/>
            <Cube currency='SGD' rate='1.5631'/>
            <Cube currency='THB' rate='39.199'/>
            <Cube currency='ZAR' rate='17.6950'/>
        </Cube>
    </Cube>
<?php
$doc = new DOMDocument();
@$doc->load('eurofxref-daily.xml');
if( $doc) {         
    $xpath = new DOMXPath($doc);
    $result = $xpath->query("//Cube[@currency='DKK']");
    for ($i=0; $i < $result->length; $i++) { 
        echo $result[$i]->getAttribute("rate")."<br>";
    }
    echo "<hr>";
    $last = $xpath->query("//Cube[@currency='DKK'][last()]");
    echo "Last:".$last[0]->getAttribute("rate")."<br>";
} 
?>

我希望有帮助...