PHP Function Reference

PHP SimpleXMLIterator - getChildren() Method



The PHP SimpleXMLIterator::getChildren() method returns a SimpleXMLIterator object containing sub-elements of the current SimpleXMLIterator element.

Syntax

public SimpleXMLIterator::getChildren()

Parameters

No parameter is required.

Return Value

Returns a SimpleXMLIterator object containing the sub-elements of the current element.

Example: returns the sub-elements of the current element

The example below shows the usage of SimpleXMLIterator::getChildren() method.

<?php
$xml = <<<XML
<userlist> 
  <user id="John123">
    <name>John Smith</name>
    <city>New York</city>
    <phone>+1-8054098000</phone>
  </user>
  
  <user id="Marry2015">
    <name>Marry G.</name>
    <city>London</city>
    <phone>+33-147996101</phone>
  </user> 
</userlist> 
XML;

$xmlIterator  = new SimpleXMLElement($xml);

for($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next()) {
  foreach($xmlIterator->getChildren() as $name => $data) {
    echo "$name: $data \n";
  }
}
?>

The output of the above code will be:

name: John Smith 
city: New York 
phone: +1-8054098000 
name: Marry G. 
city: London 
phone: +33-147996101 

❮ PHP SimpleXML Reference