Hi, does any one know a piece of code, or at least the logic behind displaying the name of every single node within an XML feed your importing? At the moment I'm manually creating loops within loops, but the problem this is causing is that there may be more layers of nodes than I'm actually taking account for. I'm using the SimpleXML class in PHP. Cheers in advance.
Does this make sense to any one? I guess a simplified way of explaining this is that I need something list all the nodes of an XML file in a similar way to how the print_r() function lists out an array. So it will list out all the fields in the correct order but so that I can actually do something with them. Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) [d] => Array ( [0] => x [1] => y [2] => Array ( [0] => Keeps going deeper... [1] => etc... ) ) ) PHP: I'm trying to create a select box with all the fields in so that I can map the XML file into my database. At the moment I am doing this: foreach($xml->children() as $child1){ foreach($child1->children() as $child2){ foreach($child2->children() as $child3){ // Obviously this could keep going for ages... } } } PHP: Really need something that can test to see if there are any children nodes under the current node selected and keep going till it stops then move on the the next parent node. Or something that can count all nodes within the feed and just list them one by one. I'm sure there must be a way to do this, I just can't work out the logic.
yes it makes sense. go here : http://php.net/manual/en/simplexmlelement.children.php first user note : Transform xml to array php second user note: run over the XML element recursivly with SimpleXML, and print it third user note: Quick easy way to turn objectified SimpleXMLElement data into an array. and count() works on simplexml. $count = count($xml->node); that should give you the missing pieces.
Yeah I've been using count() (The example was a very simple version of what I have currently got). I was having a severe mental block getting this to work though and despite being on that PHP manual page several times, I never noticed that in the user examples there were one or two methods that may help me solve my problem. Cheers for the help, I'll let you know if I run into any more problems but hopefully not now.