Marcello Impastato Marcello Impastato - 11 months ago 48
PHP Question

how get number of nodes inside a xml tag?

Having a xml file so formed:

<book id="1">
<chapter id="1">
<chapter id="2">
<chapter id="3">

How i can count nodes inside ... ?
In example above, the function should be turn 3 as result becouse present three nodes.

I access to ... so:

$xpath = new DOMXPath($filename);
$book = $xpath->query("//book[@id='1']")->item(0);

Answer Source

You can do as:

Refer: $dom->getElementsByTagName('chapter')->length;