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

时间:2021-01-05 14:01:13

this is my XML source:

这是我的XML源:

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

How can i get the latest currency rate for "DKK" (Danish Krones)?

我怎样才能获得“DKK”(丹麦克朗斯)的最新汇率?

I don't want to use foreach and check if the currency is DKK...

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

I need something like this:

我需要这样的东西:

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

Any idea how to solve this?

知道怎么解决这个问题吗?

3 个解决方案

#1


1  

You can fetch the value directly with an Xpath expression in DOM:

您可以直接使用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)");

Output:

7.4623

The XML uses a default namespace. The Cube element nodes are actually in the namespace http://www.ecb.int/vocabulary/2002-08-01/eurofxref. Xpath has no default namespace, so you have to register a prefix for it an use it in the expression.

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

  • The Xpath fetches any Cube element
    //e:Cube
  • Xpath获取任何Cube元素// e:Cube

  • Filters them for the currency attribute with the value DKK
    //e:Cube[@currency='DKK']
  • 使用值DKK // e:Cube [@ currency ='DKK']过滤货币属性的货币属性

  • Filters the result for the last node
    //e:Cube[@currency='DKK'][last()]
  • 过滤最后一个节点的结果// e:Cube [@ currency ='DKK'] [last()]

  • Gets the rate attribute node for it
    //e:Cube[@currency='DKK'][last()]/@rate
  • 获取它的rate属性节点// e:Cube [@ currency ='DKK'] [last()] / @ rate

  • Converts it into a number
    number(//e:Cube[@currency='DKK'][last()]/@rate)
  • 将其转换为数字(// e:Cube [@ currency ='DKK'] [last()] / @ rate)

The last step works only with DOMXpath::evaluate(), DOMXpath::query() and SimpleXMLElement::xpath() can only return node lists/arrays. So in SimpleXML you will have to access the first element of the result array.

最后一步仅适用于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];

#2


0  

Try this

$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'];

#3


-1  

I think there something with your xml data, because I attempt to use you data format but it seem it doesn't work, I tried to remove some entry in the xml.

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

here the test data and the code

这里是测试数据和代码

<?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>";
} 
?>

I hope that helps...

我希望有帮助......

#1


1  

You can fetch the value directly with an Xpath expression in DOM:

您可以直接使用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)");

Output:

7.4623

The XML uses a default namespace. The Cube element nodes are actually in the namespace http://www.ecb.int/vocabulary/2002-08-01/eurofxref. Xpath has no default namespace, so you have to register a prefix for it an use it in the expression.

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

  • The Xpath fetches any Cube element
    //e:Cube
  • Xpath获取任何Cube元素// e:Cube

  • Filters them for the currency attribute with the value DKK
    //e:Cube[@currency='DKK']
  • 使用值DKK // e:Cube [@ currency ='DKK']过滤货币属性的货币属性

  • Filters the result for the last node
    //e:Cube[@currency='DKK'][last()]
  • 过滤最后一个节点的结果// e:Cube [@ currency ='DKK'] [last()]

  • Gets the rate attribute node for it
    //e:Cube[@currency='DKK'][last()]/@rate
  • 获取它的rate属性节点// e:Cube [@ currency ='DKK'] [last()] / @ rate

  • Converts it into a number
    number(//e:Cube[@currency='DKK'][last()]/@rate)
  • 将其转换为数字(// e:Cube [@ currency ='DKK'] [last()] / @ rate)

The last step works only with DOMXpath::evaluate(), DOMXpath::query() and SimpleXMLElement::xpath() can only return node lists/arrays. So in SimpleXML you will have to access the first element of the result array.

最后一步仅适用于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];

#2


0  

Try this

$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'];

#3


-1  

I think there something with your xml data, because I attempt to use you data format but it seem it doesn't work, I tried to remove some entry in the xml.

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

here the test data and the code

这里是测试数据和代码

<?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>";
} 
?>

I hope that helps...

我希望有帮助......