theITvideos Posted October 26, 2010 Share Posted October 26, 2010 Hi there, I have an xml file which actually hold the currency conversion information which I downloaded from xe.com In the xml, the currency section snapshot looks like this: <currency> <csymbol>EUR</csymbol> <cname>Euro</cname> <crate>0.713</crate> <cinverse>1.403 </cinverse> </currency> this is the conversion of USD to Euro. And it tells how much is 1USD gonna be in Euros. i.e 1 USD = 0.713 EUR I have a product page on my website having the rates shown in USD. For the visitor is accessing the page from Europe it has to show the converted price in Euros. My application can detect the visitors country if hes accessing the page from Europe so that is not a problem. I just need to read the xml file and display the converted price based on the rates in the xml file. How can I read the xml file and output the price in Euros based on the rate. Thank you. All comments and feedbacks are always welcome Link to comment https://forums.phpfreaks.com/topic/216838-how-to-read-this-xml-file/ Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 here is one way to parse out the conversion rate and inverse conversion rate: <?php $xml_src = '<currency> <csymbol>EUR</csymbol> <cname>Euro</cname> <crate>0.713</crate> <cinverse>1.403 </cinverse> </currency>'; $xml = simplexml_load_string($xml_src); $crate = $xml->crate; $cinverse = $xml->cinverse; echo "crate: $crate <br />"; echo "cinverse: $cinverse <br />"; ?> output: crate: 0.713 cinverse: 1.403 Link to comment https://forums.phpfreaks.com/topic/216838-how-to-read-this-xml-file/#findComment-1126479 Share on other sites More sharing options...
theITvideos Posted October 26, 2010 Author Share Posted October 26, 2010 here is one way to parse out the conversion rate and inverse conversion rate: <?php $xml_src = '<currency> <csymbol>EUR</csymbol> <cname>Euro</cname> <crate>0.713</crate> <cinverse>1.403 </cinverse> </currency>'; $xml = simplexml_load_string($xml_src); $crate = $xml->crate; $cinverse = $xml->cinverse; echo "crate: $crate <br />"; echo "cinverse: $cinverse <br />"; ?> output: crate: 0.713 cinverse: 1.403 Thank you very much for your reply. I would like to use the where clause in reading the xml file. Or something that will return the exchange rate for the country I want. For example, if the person is sitting in China, it should search within the XML file for the chinese currency and the display the exchange rate. Please see the attached XML file for reference which has the exchange rates for the countries. Thank you [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/216838-how-to-read-this-xml-file/#findComment-1126485 Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 here is one way. i am trying to improve my xml skills, so there very well may be better: <?php $xml_file = "sample-usd.xml"; $xmlstr = file_get_contents($xml_file); $xml = new SimpleXMLElement($xmlstr); $currencies = array(); foreach ($xml->currency AS $a_currency) { $csymbol = $a_currency->csymbol; $crate = $a_currency->crate; $currencies["$csymbol"] = $crate; } // Get conversion rate for China, symbol CNY $c_rate = $currencies['CNY']; echo "Exchange rate for CNY: $c_rate <br />"; ?> Link to comment https://forums.phpfreaks.com/topic/216838-how-to-read-this-xml-file/#findComment-1126495 Share on other sites More sharing options...
theITvideos Posted October 26, 2010 Author Share Posted October 26, 2010 here is one way. i am trying to improve my xml skills, so there very well may be better: <?php $xml_file = "sample-usd.xml"; $xmlstr = file_get_contents($xml_file); $xml = new SimpleXMLElement($xmlstr); $currencies = array(); foreach ($xml->currency AS $a_currency) { $csymbol = $a_currency->csymbol; $crate = $a_currency->crate; $currencies["$csymbol"] = $crate; } // Get conversion rate for China, symbol CNY $c_rate = $currencies['CNY']; echo "Exchange rate for CNY: $c_rate <br />"; ?> I am speechless... I just love you brother!!!! Thank you so much!!! Works like magic! Thank you once again! Link to comment https://forums.phpfreaks.com/topic/216838-how-to-read-this-xml-file/#findComment-1126499 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.