开发者

SimpleXML: How to find number of children of top-level element?

开发者 https://www.devze.com 2023-01-23 05:06 出处:网络
How do I find the number of children of the root element in an XML开发者_StackOverflow document using PHP and SimpleXML?

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

DLiKS


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);


Try:

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

or

echo $xml->count();


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

0

精彩评论

暂无评论...
验证码 换一张
取 消