Jump to content

How to read this xml file


theITvideos

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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!!!  :D

 

Works like magic!

 

Thank you once again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.