Page 1 of 1

Arguments in XML hierarchy

Posted: Fri Jan 18, 2013 9:44 pm
by FrederickGeek8
OK so I want to do something like this in my PHP code, except I don't think its supported... How would I be able to do this?
$xml = simplexml_load_file("odds.xml");
$players = $xml->pool.$number->players

Re: Arguments in XML hierarchy

Posted: Sat Jan 19, 2013 2:24 am
by jacek
Not really sure what that sample is meant to be doing :? Could you post some sample data and expected result ?

Re: Arguments in XML hierarchy

Posted: Sat Jan 19, 2013 5:15 am
by FrederickGeek8
Ok I don't really want to disclose any real code but I'll give an example...
Here is a xml file
<?xml ?>
<pools>
<pool1>
<swimmers>10</swimmers>
</pool1>
<pool2>
<swimmers>15</swimmers>
</pool2>
</pools>
And so I can parse it as so
$xml = simplexml_load_file("pools.xml");
$players = $xml->pool1->swimmers;
echo $players;
but I want to place it in a function as so
<?php
function get_swimmers($pool){
$xml = simplexml_load_file("pools.xml");
$players = $xml->pool.$pool->swimmers;
echo $players;
}
?>
but that doesn't really work...

Re: Arguments in XML hierarchy

Posted: Tue Jan 29, 2013 7:42 am
by jacek
Ah you need to need to know the element name dynamically.

I believe you can do that like this
$players = $xml->{"pool{$pool}"}->swimmers;