首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用XML提取XML格式的ECB汇率时遇到一些问题。有问题的完整源代码

在使用XML提取XML格式的ECB汇率时遇到一些问题。有问题的完整源代码
EN

Stack Overflow用户
提问于 2011-08-20 04:38:45
回答 1查看 2.2K关注 0票数 2

我需要获取一个内部会计系统的历史汇率参考汇率列表。

这个问题只是关于下面的代码,如果你觉得它有用,请随意使用它(当然,一旦我们得到了解决最后的“黑”的答案。我已经在代码中包含了一个DB结构转储,所有的DB例程都被注释掉了,现在只是回显调试数据。

数据取自欧洲央行的可扩展标记语言http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml

修复了!如果您需要将历史汇率值列表拉入DB中,请随意使用此代码。确保删除echo调试并对DB查询进行排序

代码语言:javascript
运行
复制
<?php

/* DB structure:
 CREATE TABLE IF NOT EXISTS `currency_rates_history` (
  `id` int(4) NOT NULL auto_increment,
  `currency` char(3) character set utf8 collate utf8_unicode_ci NOT NULL default '',
  `rate` float NOT NULL default '0',
  `date` int(4) NOT NULL default '0',
  `est` tinyint(1) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8_unicode_ci AUTO_INCREMENT=1 ;

*/

error_reporting(E_ALL);

$table = "currency_rates_history";

$secs = '86400';

$prev_date = time();

$days = "0";

$XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml");    // European Central Bank xml only contains business days! oh well....


foreach($XML->Cube->Cube as $time)                                                          // run first loop for each date section
{

    echo "<h1>".$time["time"].'</h1>';

    list($dy,$dm,$dd) = explode("-", $time["time"]);

    $date = mktime(8,0,0,$dm,$dd,$dy);

    echo ($prev_date - $date)."<br />";

    if(($prev_date - $date) > $secs)                                                        // detect missing weekend and bank holiday values.
    {

        echo "ooh";                                                                         // for debug to search the output for missing days

        $days =(round((($prev_date - $date)/$secs),0)-1);                                   // got to remove 1 from the count....

        echo $days;                                                                         // debug, will output the number of missing days

    }
    foreach($time->Cube as $_rate)                                              // That fixed it! run the 2nd loop and ad enter the new exchange values....
    {

        $rate = floatval(str_replace(",", ".", $_rate["rate"]));


        if($days > 0)                                                                       // add the missing dates using the last known value, coul dbe more accurate but at least there is some reference data to work with
        {
            $days_cc = $days;                                                               // need to keep $days in mem for the next currency

            while($days_cc > 0)
            {
                echo $rate;
                echo date('D',$date+($days_cc*$secs))."<br />";
                /*
                mysql_query("LOCK TABLES {$table} WRITE");

                mysql_query("INSERT INTO {$table}(rate,date,currency,est) VALUES('{$rate}','".($date+($days_cc*$secs))."','{$currency}','1')");

                mysql_query("UNLOCK TABLES");
                */
                $days_cc = ($days_cc - 1);  // count down
            }
        }

        $currency = addslashes(strtolower($_rate["currency"]));
        /*
        mysql_query("LOCK TABLES {$table} WRITE");
//      mysql_query("UPDATE {$table} SET rate='{$rate}',date='{$date}' WHERE currency='{$currency}' AND date='{$date}'");   // all this double checking was crashing the script
//      if (mysql_affected_rows() == 0)
//      {
            mysql_query("INSERT INTO {$table}(rate,date,currency) VALUES('{$rate}','{$date}','{$currency}')");              // so just insert, its only going to be run once anyway!
//      }

        mysql_query("UNLOCK TABLES");
        */

        echo "1&euro;=  ".$currency."   ".$rate.", date:   ".date('D d m Y',$date)."<br/>";
    }
          $days="";                                                                         // clear days value
    $prev_date = $date;                                                                     // store the previous date
}

echo "<h1>Currencies Saved!</h1>";
?>

所以..。问题出在第二个foreach循环中: foreach($ XML ->Cube->Cube->Cube as $_rate),如果你尝试运行脚本,你会注意到日期是正确的,它很好地处理了丢失的周末和银行假日日期,但是利率值只引用了XML中的最新汇率,即今天的值。

它应该从XML中的相关区域中提取数据,即给定日期的利率...但事实并非如此。这是simplexml_load_file中的一个问题,还是我在代码中遗漏了一些愚蠢的东西?头开始疼了,所以我要休息一下。欢迎新鲜的眼球!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-20 05:42:59

第二个循环中的$XML->Cube->Cube->Cube$XML->Cube->Cube总是引用第一个第一级cube中的第一个第二级cube。因此,您在同一元素中迭代了第三级cubes。因此,你只能得到今天的利率。讲得通?

试试这个吧。我已经修改了命令行输出的代码,而不是html。唯一的主要变化是在第二个foreach语句中...

代码语言:javascript
运行
复制
$XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml");
foreach($XML->Cube->Cube as $time){
    echo "----".$time["time"]. "----\n";
    foreach($time->Cube as $_rate){
        $rate = floatval(str_replace(",", ".", $_rate["rate"]));
        $currency = addslashes(strtolower($_rate["currency"]));
        echo "1 euro =  ".$currency."   ".$rate . "\n";
    }
    echo "------------------\n\n";
}
echo "Done!";
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7127144

复制
相关文章

相似问题

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