Reading an XML file and store data to mysql database

Deadly 提交于 2019-12-11 14:22:44

问题


Hi I need the following php script to do a currency conversion using a different XML file.

Its a script from white-hat design http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php

The script needs to be amended to do the following:

1, The php script downloads every 24 hours an xml file from rss.timegenie.com/foreign_exchange_rates_forex

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

2, It then stores the xml file data/contents to a mysql database file ie currency and rate.

Any advice would be appreciated.


回答1:


Have a look at simpleXML load-string or load-file. Using this you can parse the XML contents and extract the string. For e.g. (from PHP manuals)

<?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);
?>

This will output:

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

Let know how it goes.




回答2:


The script from white hat design uses a regular expression to extract the currency and rate.

As pinaki mentioned your best bet is to use a more simpler and flexible solution of loading the xml file and parsing it with the simplexml functions. The examples on php.net should get you going. Practice on the following xml and when you can parse this then you you'll easily be able to tackle the real xml from timegenie.com :

<data>
<code>AOA</code>
<description>Angola Kwanza</description>
<rate>125.17</rate>
</data>


来源:https://stackoverflow.com/questions/2599597/reading-an-xml-file-and-store-data-to-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!