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?
[syntax=php]$xml = simplexml_load_file("odds.xml");
$players = $xml->pool.$number->players[/syntax]

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
[syntax=xhtml]<?xml ?>
<pools>
<pool1>
<swimmers>10</swimmers>
</pool1>
<pool2>
<swimmers>15</swimmers>
</pool2>
</pools>[/syntax]

And so I can parse it as so
[syntax=php]
$xml = simplexml_load_file("pools.xml");
$players = $xml->pool1->swimmers;
echo $players;
[/syntax]

but I want to place it in a function as so
[syntax=php]
<?php
function get_swimmers($pool){
$xml = simplexml_load_file("pools.xml");
$players = $xml->pool.$pool->swimmers;
echo $players;
}
?>
[/syntax]
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

[syntax=php]$players = $xml->{"pool{$pool}"}->swimmers;[/syntax]