Page 1 of 1

SimpleXML query

Posted: Fri Nov 07, 2008 1:16 pm
by okelly
I'm still struggling with this I'm afaid. I'm trying to get simplexml to read the remote url here http://www.ecb.europa.eu/stats/eurofxre ... -daily.xml and return a html array of currencies and rates.
The code below I use renders a blank page. any ideas anybody? thanks Conor

Code: Select all

 
<?php // Load and parse the XML document 
$rss =  simplexml_load_file('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'); ?>
<html xml:lang="en" lang="en">
<head> </head>
<body>
<?php
// Here we'll put a loop to include each item's title and description
foreach ($rss->gesmes:Envelope->Cube as $item) {
  echo "<p>" . $item->currency ."</p>";
  echo "<p>" .$item->rate . "</p>";
}
?>
</body>
</html>
 
 
 

Re: SimpleXML query

Posted: Fri Nov 07, 2008 5:12 pm
by requinix
You still don't know how to use SimpleXML? Figure it out - you're way off track.

Code: Select all

<?php
// Here we'll put a loop to include each item's title and description
foreach ($rss->Cube->Cube->Cube as $item) {
  echo "<p>" . $item["currency"] ."</p>";
  echo "<p>" .$item["rate"] . "</p>";
}
?>
  1. Does "gesmes:Envelope" look like a variable name to you? It's not.
  2. $rss is the top-most element. $rss->Envelope doesn't exist.
  3. If you looked at the XML you'd see that the Cubes you want are nested three down, not one.
  4. -> is for elements, [] is for attributes.