读取XML文件并将数据存储到mysql数据库

时间:2010-04-08 12:02:12

标签: php mysql xml currency

您好我需要以下PHP脚本才能使用不同的XML文件进行货币转换。

它是白帽设计的脚本 http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php

需要修改脚本以执行以下操作:

1,php脚本每24小时下载一个xml文件 rss.timegenie.com/foreign_exchange_rates_forex

rss.timegenie.com/forex.xml或rss.timegenie.com/forex2.xml

2,然后将xml文件数据/内容存储到mysql数据库文件中,即货币和费率。

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

看看simpleXML load-string or load-file。使用它,您可以解析XML内容并提取字符串。对于例如(来自PHP手册)

<?php
$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

var_dump($xml);
?>

这将输出:

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

让我们知道它是怎么回事。

答案 1 :(得分:0)

白帽设计的脚本使用正则表达式来提取货币和汇率。

正如pinaki提到的,​​最好的办法是使用更简单灵活的加载xml文件并使用simplexml函数解析它的解决方案。 php.net上的例子可以帮到你。练习下面的xml,当你可以解析它时,你就可以轻松地从timegenie.com处理真正的xml:

<data>
<code>AOA</code>
<description>Angola Kwanza</description>
<rate>125.17</rate>
</data>
相关问题