Php parse xml

Hi all,

I want to grab the currency rates from the European Central Bank. I would also like all the money, but thats a different job.

http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

My syntax isn’t quite right.

Somebody help, and I’ll offer you a place on the team for the other “job”.

<?
$currency_url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";

$xml = simplexml_load_file($currency_url) or die("feed not loading");

$USD = $xml->cube->Cube currency->"USD";
$GBP = $xml->cube-><Cube currency="GBP" 

echo " Dollar rate is $USD <br>";
echo "Pound rate is $GBP<br>";
?>

…still on this I’m afraid … struggling to get some good examples - of course oddz posted an example but I got a little confused as to how I could apply it to my situation… which is (as I posted a few back):

$first = $xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]");

$second = $xml_2->xpath(".//*[@day]");

$arr = array($first, $second);

for($x=0; $x<=1; $x++) { 

    foreach($arr[$x] as $item){

        $it1 = array($item['name'], $item['day']);

        $it2 = array($item['backp1'], $item['amount']); // these are numbers but may need converting


        printf('%s = %f<br />', $it1[$x], $it2[$x]);

    }

} 

…any similar examples or further pushes are very welcome right now <<

_thanks all

thanks! - is this the general consensus? … I was hoping there was a strictly PHP way of sorting the XML but if not XSL it is <<

_with thanks

Thanks… I’m still struggling - let me show you what I’m trying so maybe you can see what’s going wrong. Imagine I’m getting feeds from 2 different sites and my objective is to display them in ascending order:

$first = $xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]");
$second = $xml_2->xpath(".//*[@day]");

$arr = array($first, $second);

for($x=0; $x<=1; $x++) { 

	foreach($arr[$x] as $item){

		$it1 = array($item['name'], $item['day']);
		$it2 = array($item['backp1'], $item['amount']); // these are numbers but may need converting

		sort($it2); // NOT working as it should
		
		printf('%s = %f<br />', $it1[$x], $it2[$x]);

	}
} 

…I know that both of the paths work and the correct information is displayed from both feeds but I am unable to sort it >> ‘$it1[0]’ returns ‘0’ when I have ‘sort($it2)’ and as it should when this is removed…

>> can anyone spot the problem or if you have a suggestion I would love to hear it <<

_with thanks to all

Does anyone know if there is a way to put the xml data into arrays and then sort it? I put it into an array but wasn’t able to sort it because I don’t think it’s coming through as numbers but (int) didn’t work in order to convert it…

Any help on this is appreciated!

_with thanks

There are a couple of different approaches to sorting the SimpleXML elements. One would be to literally loop over the items, pushing their values (or a subset of) into an array then sorting that array. Another would be to use SPL classes to sort the SimpleXML objects themselves (though this may be a big leap, conceptually, if you’ve not used this kind of thing before).

… because the others weren’t the correct directory syntax<

I think the easiest way to sort XML is to use XSL.

The short of it is to build a XSL string and use it to transform the XML as needed. Although if you didn’t need to change the sorting the XSL could actually be a separate file.

Very clear response thanks!!

I went with this:

foreach($xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]") as $item) {
    printf('%s = %f<br />', $item['name'], $item['backp1']);
}

_thanks!

Do you understand why that works, where others didn’t?

One popular way to troubleshoot this type of situation is to:

[list=1][]broaden the selector
[
]narrow the requirements
[*]and then simplify
[/list]

First you get it working, by using a broader selector:

“//subevent/selection”


foreach($xml->xpath("//subevent/selection") as $item) {
    printf('&#37;s = %f<br />', $item['name'], $item['backp1']);
}


July = 2.200000
August = 2.800000
November = 2.400000
December = 1.900000

That gives all months, so now we narrow the requirements by adding in attribute selectors:

“//subevent[@title=‘Hottest day of the year’]/selection[@name]”


foreach($xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]") as $item) {
    printf('%s = %f<br />', $item['name'], $item['backp1']);
}


July = 2.200000
August = 2.800000

Now you can simplify by using * for the node names, and end up in this case with something close to what you started with in the first place:

“//[@title=‘Hottest day of the year’]/[@name]”

This provides the same output as before:


foreach($xml->xpath("//*[@title='Hottest day of the year']/*[@name]") as $item) {
    printf('%s = %f<br />', $item['name'], $item['backp1']);
}


July = 2.200000
August = 2.800000

thanks for the response >> for some reason it’s not doing it for me and although the link you posted seems useful I still can’t seem to get it right…

Thanks for any further insight anyone can lend<

_

A good overview of XPath in PHP can be found [url=http://schlitt.info/opensource/blog/0704_xpath.html]here.

For your latest query, something like //subevent[@title=“Hottest day of the year”] should suffice.

That aside … I’m struggling a little to reference the path for my own customised attempts at the feeds…

Suppose we had this:


<subevent title="Hottest day of the year" id="97383">
   <selection name="July" id="53" backp1="2.20"/>
   <selection name="August" id="54" backp1="2.80"/>
</subevent>
<subevent title="Coldest day of the year" id="97384">
   <selection name="November" id="55" backp1="2.40"/>
   <selection name="December" id="56" backp1="1.90"/>
</subevent>


…and in trying to get back all of the ‘selection name’ and ‘backp1’ in ‘Hottest day of the year’ my path is currently:

foreach($xml->xpath(".//*[title='Hottest day of the year']/[@name]") as $item){
    printf('%s = %f<br />', $item['name'], $item['backp1']);
}

Of course this doesn’t work >> I have been using http://www.w3schools.com/xpath/xpath_syntax.asp but cannot find the right combination…

Thanks for any help that anyone can offer!

_

fair point -> 2-0

Ha, thanks! However, I would still say I was technically correct. :stuck_out_tongue:

you would assume wrong - my bad!! - for some reason it was set to 4. Good reflexes though Sterling!

_thanks

Looks good … not sure why but I’m getting the error message "Cannot instantiate non-existent class: simplexmlelement in … ". Can anyone suggest as to why?

_with thanks

I would assume you’re running PHP with a version < 5.0.1 ?

http://www.php.net/manual/en/simplexmlelement.construct.php

At http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html they have a tab called “For Developers” that gives you PHP code to successfully parse the file.