Arguments in XML hierarchy

Ask about a PHP problem here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Arguments in XML hierarchy

Post 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]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Arguments in XML hierarchy

Post by jacek »

Not really sure what that sample is meant to be doing :? Could you post some sample data and expected result ?
Image
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Arguments in XML hierarchy

Post 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...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Arguments in XML hierarchy

Post 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]
Image
Post Reply