Reading an XML file and store data to mysql database
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 e开发者_Go百科very 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.
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.
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>
精彩评论