tags:

views:

41

answers:

1

how do i get the list of children in xml->request->ABC ABC may have DEF, ZZA, XAS, ETC and i would like to iterate through a list of these children (name required) instead of checking if they exist.

-edit- Note: I am looking for the element name. I found an example which returns an attribute IF its known. How do i get the tag/element name?

+2  A: 

Considering this piece of XML and the code to load it with SimpleXML :

$str = <<<XML
<xml>
    <request>
        <ABC>
            <DEF>glop</DEF>
            <ZZA>test</ZZA>
        </ABC>
    </request>
</xml>
XML;
$xml = simplexml_load_string($str);

What about using the children() method to get the list of all children of you ABC element, and loop over them with a foreach ?

This way, for instance :

foreach ($xml->request->ABC->children() as $a => $b) {
    echo "$a $b<br />";
}

And you'll get this kind of output :

DEF glop
ZZA test
Pascal MARTIN