$xml = new SimpleXMLElement(''); $count = $xml->count(); echo "Number of books: $count"; PHP Cookbook JavaScript: The Good Parts
Number of books: 2
$xml = new SimpleXMLElement(''); $count = $xml->xpath('/root/book/title')->count(); echo "Number of book titles: $count"; PHP Cookbook JavaScript: The Good Parts
Number of book titles: 2In this example, we use the `xpath()` method to select all `title` elements under the `book` elements. We then call the `count()` method on the resulting `SimpleXMLElement` object, which returns the total number of matched elements (in this case, two `title` elements). The `SimpleXMLElement` class is part of the PHP core, so no external libraries or packages are required to use it.