SimpleXMLIterator::hasChildren

(PHP 5 >= 5.1.0, PHP 7)
Checks whether the current element has sub elements.
public bool SimpleXMLIterator::hasChildren ( void )

This method checks whether the current SimpleXMLIterator element has sub-elements.

Returns:

TRUE if the current element has sub-elements, otherwise FALSE

Examples:
Check whether the current element has sub-elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$xml = <<<XML
<books>
    <book>
        <title>PHP Basics</title>
        <author>Jim Smith</author>
    </book>
    <book>XML basics</book>
</books>
XML;
 
$xmlIterator new SimpleXMLIterator( $xml );
for$xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
    if($xmlIterator->hasChildren()) {
        var_dump($xmlIterator->current());
    }
}
?>

The above example will output:

object(SimpleXMLIterator)#2 (2) {
  ["title"]=>
  string(10) "PHP Basics"
  ["author"]=>
  string(9) "Jim Smith"
}
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.