tags:

views:

68

answers:

1

Using PHP's SimpleXML i would like to get the key and child of an element. The first element if there are more then one. How do i do this? the 2nd line doesnt make sense so how would get the first key/val of the first element?

$body = $xml->Body;
$xml->Body->children() as $XX=>$ZZ;
echo "x $XX $ZZ\n";
foreach($xml->Body->children() as $k=>$v){
 echo "$k $v\n";
}
+1  A: 

Can be done via IteratorIterator.
Or (even simpler) by treating the return value of children() (almost) like an array:

$xml = new SimpleXMLElement('<foo><Body>
  <a>001</a>
  <b>002</b>
  <c>003</c>
</Body></foo>');

$c = $xml->Body->children();
if ( isset($c[0]) ) {
  echo $c->getName(), " : ", (string)$c;
}
VolkerK