Hello,
just a note for people like me who encounter the problems :
<?php
foreach($xmlIterator as $x)
{
if($x->hasChildren())
{
//never reach
echo 'x : ';
var_dump($x->current());
}
if($xmlIterator->hasChildren())
{
echo 'iterator : ';
var_dump($x->current()); //all time null
var_dump($xmlIterator->current());
}
}
?>
SimpleXMLIterator::hasChildren
(PHP 5 >= 5.1.0)
SimpleXMLIterator::hasChildren — Comprueba si el elemento actual tiene sub elemento
Descripción
bool SimpleXMLIterator::hasChildren
( void
)
Este método comprueba si el elemento SimpleXMLIterator actual tiene sub elementos.
Parámetros
Esta función no tiene parámetros.
Valores devueltos
TRUE si el elemento actual tiene sub elementos, en caso contrario FALSE
Ejemplos
Ejemplo #1 Comprobar si el elemento actual tiene sub elementos
<?php
$xml = <<<XML
<books>
<book>
<title>PHP Básico</title>
<author>Jim Smith</author>
</book>
<book>XML básico</book>
</books>
XML;
$xmlIterator = new SimpleXMLIterator( $xml );
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
if($xmlIterator->hasChildren()) {
var_dump($xmlIterator->current());
}
}
?>
El resultado del ejemplo sería:
object(SimpleXMLIterator)#2 (2) {
["title"]=>
string(10) "PHP Básico"
["author"]=>
string(9) "Jim Smith"
}
grummfy at gmail dot com ¶
3 years ago
