PHP getChildren() 函数
实例
获取当前元素的子元素并输出名称和数据:
<?php
$bookxml = <<<XML
<bookstore>
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</bookstore>
XML;
$xml = new SimpleXMLIterator($bookxml);
for( $xml->rewind(); $xml->valid(); $xml->next() ) {
foreach($xml->getChildren() as $name => $data) {
echo "The $name is '$data'";
echo "<br>";
}
}
?>
运行实例 »
$bookxml = <<<XML
<bookstore>
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</bookstore>
XML;
$xml = new SimpleXMLIterator($bookxml);
for( $xml->rewind(); $xml->valid(); $xml->next() ) {
foreach($xml->getChildren() as $name => $data) {
echo "The $name is '$data'";
echo "<br>";
}
}
?>
定义和用法
getChildren() 函数返回当前元素的子元素。
语法
SimpleXMLIterator::getChildren()
技术细节
返回值: | 包含当前元素的子元素的 SimpleXMLIterator 对象 |
---|---|
PHP 版本: | 5.0+ |