PHP Function Reference

PHP SimpleXMLIterator - next() Method



The PHP SimpleXMLIterator::next() method moves the SimpleXMLIterator to the next element.

Syntax

public SimpleXMLIterator::next()

Parameters

No parameter is required.

Return Value

No value is returned.

Example:

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

<?php
$xml = <<<XML
<mail> 
  <To>John Smith</To>
  <From>Marry G.</From>
  <Subject>Happy Birthday</Subject>
  <body>Happy birthday. Live your life with smiles.</body>
</mail> 
XML;

$xmlIterator  = new SimpleXMLElement($xml);

//rewind to first element
$xmlIterator->rewind(); 

//return the current element
var_dump($xmlIterator->current());

//advance to the next element
$xmlIterator->next(); 

//return the current element
var_dump($xmlIterator->current());
?>

The output of the above code will be:

object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(10) "John Smith"
}
object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(8) "Marry G."
}

❮ PHP SimpleXML Reference