php - How to iterate through an XML element node with dynamic children -


i have following xml structure:

<root>   <maininfo>     <node>       <tournament_id>3100423</tournament_id>       <games>         <a_0>           <id>23523636</id>           <type>             <choice_4>               <choice_id>345</choice_id>             <choice_4>             <choice_9>               <choice_id>345</choice_id>             <choice_9>             ... etc           </type>         </a_0>         <a_1></a_1>         <a_2></a_2>         ...etc       </games>     </info>   </node> </root> 

i can id of first node element "a_0" doing:

maininfo[0]->a_3130432[0]->games[0]->a_1[0]->id; 

my issue is: how automatically iterate (with foreach) through a_0, a_1, a_2 , values of each of these node elements , of children "345" in <choice_id>345</choice_id>?

the ending numbers of a_0, a_1 + children of choice_4, choice_9, dynamically created , there no logic in _[number] counting +1 each next element.

as has been outlined on stackoverflow (for example in read xml dynamic php) , in php manual (for example in basic simplexml usage), can iterate on child elements using foreach.

for example go on a_* elements, it's just

foreach ($xml->maininfo->node->games[0] $name => $a) {     echo $name, "\n"; } 

output:

a_0 a_1 a_2 

you want iterate on these ->type children again. is possible in pure php putting 1 foreach another:

foreach ($xml->maininfo->node->games[0] $name => $a) {     echo $name, "\n";      if (!$a->type[0]) {         continue;     }      foreach ($a->type[0] $name => $choice) {         echo ' +- ', $name, "\n";     } } 

this outputs:

a_0  +- choice_4  +- choice_9 a_1 a_2 

this starts bit complicated. can imagine since xml famous it's tree structures, you're not first 1 running problem. therefore query-language elements xml document has been invented: xpath.

with xpath can access xml data if file-system. know each a_* element child of games , each choice_* element child of type, it's pretty straight forward:

 /*/maininfo/node/games/*/type/*   ^                     ^      ^   |                     |   choice_* root                    |                        a_* 

in php simplexml looks like:

$choices = $xml->xpath('/*/maininfo/node/games/*/type/*');  foreach ($choices $choice) {     echo $choice->getname(), ': ', $choice->choice_id, "\n"; } 

output:

choice_4: 345 choice_9: 345 

as example shows, data retrieved single foreach.

if need access <a_*> elements, need have multiple foreach's or own iteration more advanced topic i'd extend on limits of question.

i hope helpful far. see simplexmlelement::children() gives children (like ->games[0] in first example). example codes available a working, interactive online-demo.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -