0

I'm quite new to xml and php and i have trouble how to display metals in usd currency only from xml feed in an external url http://www.xmlcharts.com/cache/precious-metals.php

<prices>
<currency access="aud">
<price access="gold">47.434995697675</price>
<price access="palladium">26.322725436389</price>
<price access="platinum">51.924285690151</price>
<price access="silver">0.71125071751027</price>
</currency>
<currency access="brl">
<price access="gold">96.721773294575</price>
<price access="palladium">53.673045495365</price>
<price access="platinum">105.87560755807</price>
<price access="silver">1.4502674585044</price>
</currency>
<currency access="cad">
<price access="gold">43.634985906498</price>
<price access="palladium">24.214016182439</price>
<price access="platinum">47.764639607752</price>
<price access="silver">0.65427253819838</price>
</currency>
<currency access="chf">
<price access="gold">39.490112355758</price>
<price access="palladium">21.913934421285</price>
<price access="platinum">43.227491554238</price>
<price access="silver">0.59212339612314</price>
</currency>
<currency access="cny">
<price access="gold">259.21346527132</price>
<price access="palladium">143.84326962395</price>
<price access="platinum">283.74565713604</price>
<price access="silver">3.8867034865481</price>
</currency>
<currency access="eur">
<price access="gold">31.961694805017</price>
<price access="palladium">17.736249460129</price>
<price access="platinum">34.986577900722</price>
<price access="silver">0.47924065404789</price>
</currency>
<currency access="gbp">
<price access="gold">27.893018510833</price>
<price access="palladium">15.4784512374</price>
<price access="platinum">30.53283847959</price>
<price access="silver">0.41823403033067</price>
</currency>
<currency access="inr">
<price access="gold">2558.8559365892</price>
<price access="palladium">1419.9656025986</price>
<price access="platinum">2801.0283280769</price>
<price access="silver">38.368046505244</price>
</currency>
<currency access="jpy">
<price access="gold">4196.440446</price>
<price access="palladium">2328.6973688</price>
<price access="platinum">4593.5952854</price>
<price access="silver">62.9223474</price>
</currency>
<currency access="mxn">
<price access="gold">540.37670151162</price>
<price access="palladium">299.86695132785</price>
<price access="platinum">591.51843100021</price>
<price access="silver">8.1025266477426</price>
</currency>
<currency access="rub">
<price access="gold">1399.5182262463</price>
<price access="palladium">776.62353439423</price>
<price access="platinum">1531.9698703324</price>
<price access="silver">20.984682889624</price>
</currency>
<currency access="usd">
<price access="gold">42.2943</price>
<price access="palladium">23.47004</price>
<price access="platinum">46.29707</price>
<price access="silver">0.63417</price>
</currency>
<currency access="zar">
<price access="gold">421.22164931813</price>
<price access="palladium">233.74518453698</price>
<price access="platinum">461.08643916549</price>
<price access="silver">6.315889690764</price>
</currency>
</prices>

I have used PHP for loading this, here's the code.

    <?php header('Content-Type: text/plain'); 
$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
if ($content === false) die('Something went wrong.'); 
foreach (json_decode($content, true) as $currency["11"] => $arr) 
    { 
foreach ($arr as $commodity => $price) 
    { 
        print $commodity.' '.round($price, 2); 
    }  
    } 
?>

And my target output is

gold 42.29 palladium 23.47 platinum 46.3 silver 0.63

  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Pekka Aug 01 '13 at 20:29
  • 1
    Why are you using json_decode for XML? Use the SimpleXMLElement class. – Ben Fortune Aug 01 '13 at 20:29
  • He isn't actually using XML format. Notice the `?format=json` – Kirk Backus Aug 01 '13 at 20:32
  • Ah, my eyes are deceiving me again. – Ben Fortune Aug 01 '13 at 20:33
  • 2
    @KirkBackus Actually, I noticed the large block of XML data, rather than the query string scrolled off the page. Perhaps the OP should have posted a more consistent question. +1 for your answer, just for spotting the non-obvious! –  Aug 01 '13 at 20:39

3 Answers3

4

Like this!

$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
$metals = json_decode($content);
echo 'gold '.$metals->usd->gold;
echo 'palladium '.$metals->usd->palladium;
echo 'platinum '.$metals->usd->platinum;
echo 'silver '.$metals->usd->silver;
Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
  • You may want to also add a quick explanation that he's actually getting the data in the JSON format - seems to be some confusion! – Hannele Aug 01 '13 at 20:49
0
$data = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json');
$array = json_decode($data, true);
foreach ($array['usd'] as $metal => $price) {
    echo $metal . ' ' . round($price, 2) . ' ';
}
klkvsk
  • 670
  • 4
  • 7
0

To display usd only:

$data = json_decode($content, true);
foreach($data['usd'] as $commodity => $price) 
{
    print $commodity.' '.round($price, 2); 
}
Hubu
  • 59
  • 2