Marcello Impastato Marcello Impastato - 24 days ago 9
PHP Question

how get number of nodes inside a xml tag?

Having a xml file so formed:

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


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

NOTE:
I access to ... so:

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

Answer

You can do as:

Refer: http://de2.php.net/manual/en/book.dom.php $dom->getElementsByTagName('chapter')->length;

unless

$xpath->evaluate('count(//book/chapter');
Comments