Example 1 — Check Each Child
Move through the children of library and ask whether each one has children of its own.
<?php
$xml = simplexml_load_string(
'<library><book><title>PHP</title></book><note>Hello</note></library>'
);
$xml->rewind();
echo $xml->key(), ': ', var_export($xml->hasChildren(), true), PHP_EOL;
$xml->next();
echo $xml->key(), ': ', var_export($xml->hasChildren(), true), PHP_EOL; How It Works
- The library root has two children: book, which holds a title element, and note, which holds only text.
- After rewind(), the iterator points to book, and hasChildren() is true because of the title element.
- next() moves the iterator to note.
- hasChildren() is false for note, because text is not a child element.

