views:

36

answers:

3

Hi!

How do I find the number of children of the root element in an XML document using PHP and SimpleXML?

DLiKS

+5  A: 

If you are using PHP 5.3+ you can use SimpleXMLElement::count

Otherwise, just do somthing like:

$sxe = new SimpleXMLElement($xml);
$kids = $sxe->children();
echo count($kids);
Byron Whitlock
A: 

Try:

$xml = simplexml_load_string( $string );
echo count( $xml );

or

echo $xml->count();
Harmen
A: 

SimpleXML represent XML nodes as arrays, so, you can simple use count() with simplexml object as argument count(simplexml_load_string( $string ))

seriyPS