文字
分享

SimpleXMLIterator::hasChildren

(PHP 5 >= 5.1.0)

SimpleXMLIterator::hasChildrenChecks whether the current element has sub elements.

说明

public bool SimpleXMLIterator::hasChildren ( void )

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

参数

此函数没有参数。

返回值

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

范例

Example #1 Check whether the current element has sub-elements

<?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 ());
    }
}
?>

以上例程会输出:

1

2

3

4

5

6

object(SimpleXMLIterator)#2 (2) {

  ["title"]=>

  string(10) "PHP Basics"

  ["author"]=>

  string(9) "Jim Smith"

}

用户评论:

[#1] grummfy at gmail dot com [2010-02-12 01:01:57]

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());
    }
}
?>