SimpleXML query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
okelly
Forum Commoner
Posts: 29
Joined: Thu Feb 23, 2006 2:18 pm

SimpleXML query

Post 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>
 
 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SimpleXML query

Post 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.
Post Reply