首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php解析来自陌生xml的数据

php解析来自陌生xml的数据
EN

Stack Overflow用户
提问于 2014-10-23 15:20:09
回答 3查看 671关注 0票数 0

大家好,有人能帮我解析来自以下xml的数据吗?

代码语言:javascript
复制
 <?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='2014-10-23'>
        <Cube currency='USD' rate='1.2669'/>
        <Cube currency='JPY' rate='136.42'/>
        <Cube currency='BGN' rate='1.9558'/>
        <Cube currency='CZK' rate='27.690'/>
        <Cube currency='DKK' rate='7.4462'/>
        <Cube currency='GBP' rate='0.79040'/>
        <Cube currency='HUF' rate='307.40'/>
        <Cube currency='LTL' rate='3.4528'/>
        <Cube currency='PLN' rate='4.2290'/>
        <Cube currency='RON' rate='4.4245'/>
        <Cube currency='SEK' rate='9.1869'/>
        <Cube currency='CHF' rate='1.2067'/>
        <Cube currency='NOK' rate='8.3090'/>
        <Cube currency='HRK' rate='7.6733'/>
        <Cube currency='RUB' rate='52.6736'/>
        <Cube currency='TRY' rate='2.8336'/>
        <Cube currency='AUD' rate='1.4404'/>
        <Cube currency='BRL' rate='3.1596'/>
        <Cube currency='CAD' rate='1.4208'/>
        <Cube currency='CNY' rate='7.7542'/>
        <Cube currency='HKD' rate='9.8279'/>
        <Cube currency='IDR' rate='15257.82'/>
        <Cube currency='ILS' rate='4.7737'/>
        <Cube currency='INR' rate='77.5153'/>
        <Cube currency='KRW' rate='1339.01'/>
        <Cube currency='MXN' rate='17.1095'/>
        <Cube currency='MYR' rate='4.1440'/>
        <Cube currency='NZD' rate='1.6115'/>
        <Cube currency='PHP' rate='56.775'/>
        <Cube currency='SGD' rate='1.6134'/>
        <Cube currency='THB' rate='40.993'/>
        <Cube currency='ZAR' rate='13.8557'/>
    </Cube>
</Cube>

我正在尝试使用以下代码获取货币=‘CZK’及其速率

代码语言:javascript
复制
 $ch = curl_init();
 $cv = curl_version();
  $user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']}         ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; 
 curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
 curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity");
 curl_setopt($ch, CURLOPT_HEADER, FALSE);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
 curl_setopt($ch, CURLOPT_URL, "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
 $xml_data = curl_exec($ch);
 curl_close($ch);

 echo $xml_data;
    $parser = simplexml_load_string($xml_data);
echo $parser; 

$xml_data的唯一结果是“参考利率--欧洲央行”,而$parser --什么也没有

为什么$xml_data的结果仅仅是“参考利率欧洲中央银行”而不是其他的xml文件?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-23 15:49:26

正如Ian已经指出的,$parser是一个对象,所以您不能简单地回显它。

如果您只在“CZK”的值之后,则可以使用以下代码:

代码语言:javascript
复制
 $ch = curl_init();
 $cv = curl_version();
 $user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']}         ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; 
 curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
 curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity");
 curl_setopt($ch, CURLOPT_HEADER, FALSE);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
 curl_setopt($ch, CURLOPT_URL, "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
 $xml_data = curl_exec($ch);
 curl_close($ch);

 $parser = simplexml_load_string($xml_data);

 // Echo the rate for CZK
 echo $parser->Cube->Cube->Cube[3]['rate'];

 // Echo the currency 
 echo $parser->Cube->Cube->Cube[3]['currency'];

显然,上面的代码片段依赖于XML提要,每次都以相同的顺序返回数据。您应该考虑根据属性选择正确的节点,以便每次选择正确的元素。

希望有帮助:)

票数 1
EN

Stack Overflow用户

发布于 2014-10-23 15:38:07

根据文档simplexml_load_string将XML字符串解释为对象。Echo不会显示完整的对象,因此您需要使用print_r($parser)来查看页面上XML文件中的所有数据(尽管使用xdebug这样的调试器效率要高得多,并且允许您查看变量中的数据而不将它们回显到页面上)。

您将看到每个多维数据集都表示为对象,然后它们的属性是数组。您应该能够轻松地遍历并获取所需的数据。

您可以使用echo '<pre>' . print_r( $parser, true ) . '</pre>';获得下面的完整对象树

代码语言:javascript
复制
SimpleXMLElement Object
(
    [Cube] => SimpleXMLElement Object
        (
            [Cube] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [time] => 2014-10-23
                        )

                    [Cube] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => USD
                                            [rate] => 1.2669
                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => JPY
                                            [rate] => 136.42
                                        )

                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => BGN
                                            [rate] => 1.9558
                                        )

                                )

                            [3] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CZK
                                            [rate] => 27.690
                                        )

                                )

                            [4] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => DKK
                                            [rate] => 7.4462
                                        )

                                )

                            [5] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => GBP
                                            [rate] => 0.79040
                                        )

                                )

                            [6] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HUF
                                            [rate] => 307.40
                                        )

                                )

                            [7] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => LTL
                                            [rate] => 3.4528
                                        )

                                )

                            [8] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => PLN
                                            [rate] => 4.2290
                                        )

                                )

                            [9] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => RON
                                            [rate] => 4.4245
                                        )

                                )

                            [10] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => SEK
                                            [rate] => 9.1869
                                        )

                                )

                            [11] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CHF
                                            [rate] => 1.2067
                                        )

                                )

                            [12] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => NOK
                                            [rate] => 8.3090
                                        )

                                )

                            [13] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HRK
                                            [rate] => 7.6733
                                        )

                                )

                            [14] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => RUB
                                            [rate] => 52.6736
                                        )

                                )

                            [15] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => TRY
                                            [rate] => 2.8336
                                        )

                                )

                            [16] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => AUD
                                            [rate] => 1.4404
                                        )

                                )

                            [17] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => BRL
                                            [rate] => 3.1596
                                        )

                                )

                            [18] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CAD
                                            [rate] => 1.4208
                                        )

                                )

                            [19] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CNY
                                            [rate] => 7.7542
                                        )

                                )

                            [20] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HKD
                                            [rate] => 9.8279
                                        )

                                )

                            [21] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => IDR
                                            [rate] => 15257.82
                                        )

                                )

                            [22] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => ILS
                                            [rate] => 4.7737
                                        )

                                )

                            [23] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => INR
                                            [rate] => 77.5153
                                        )

                                )

                            [24] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => KRW
                                            [rate] => 1339.01
                                        )

                                )

                            [25] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => MXN
                                            [rate] => 17.1095
                                        )

                                )

                            [26] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => MYR
                                            [rate] => 4.1440
                                        )

                                )

                            [27] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => NZD
                                            [rate] => 1.6115
                                        )

                                )

                            [28] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => PHP
                                            [rate] => 56.775
                                        )

                                )

                            [29] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => SGD
                                            [rate] => 1.6134
                                        )

                                )

                            [30] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => THB
                                            [rate] => 40.993
                                        )

                                )

                            [31] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => ZAR
                                            [rate] => 13.8557
                                        )

                                )

                        )

                )

        )

)
票数 0
EN

Stack Overflow用户

发布于 2015-11-03 04:35:53

我实际上使用了这家银行的xml。我很高兴它也给出了90天的历史xml。我对此有不同的看法。我创建了一个数组,以所有货币作为键,并使它们与汇率相等。然后,我可以使用我的数组获得如下的速率:

代码语言:javascript
复制
$xml=file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

// base rate EUR    
$_RATES['EUR']=1;

foreach($xml as $line){
  if(preg_match("/currency='([[:alpha:]]+)'/",$line,$currency)){
    if(preg_match("/rate='([[:graph:]]+)'/", $line,$rate)){$_RATES[$currency[1]]=$rate[1];}
  }
}

现在你的利率是$_RATES["CZK"]

这很容易让您轻松地将它兑换成任何货币。例如,如果您希望从美元到CZK之间的汇率并得到交换的值,只需这样做:

代码语言:javascript
复制
$converted = ($_RATES[$currency_to]/$_RATES[$currency_from]) * $value; 

希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26531501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档