PHP - SimpleXMLIterator::getChildren() 函数



定义和用法

XML 是一种用于在网络上共享数据的标记语言,XML 既可供人类阅读,也可供机器阅读。SimpleXMLIterator 用于迭代 XML 文档的所有节点。

SimpleXMLIterator::getChildren() 函数将迭代器的当前元素的子元素作为 SimpleXMLElement 类的对象返回。

语法

SimpleXMLIterator::getChildren(void);

参数

此函数不接受任何参数。

返回值

此函数返回一个布尔值,如果迭代器的当前元素具有子元素,则为 TRUE;否则为 FALSE。

PHP 版本

此函数首次在 PHP 5 版本中引入,并在所有后续版本中均有效。

示例

以下示例演示了 SimpleXMLIterator::getChildren() 函数的用法。

<html>
   <head>
      <body>
         <?php
            $doc=new DOMDocument;
            $data="<Tutorials>
            <Tutorial>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>

            <Tutorial>
               <Name>CoffeeScript</Name>
               <Pages>235</Pages>
               <Author>Kasyap</Author>
               <Version>2.5.1</Version>
               </Tutorial>
            </Tutorials>";
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator($data);
            $xmlIterator->rewind();
            
            //Retrieving the iterator
            $res = $xmlIterator->getChildren();
            print_r($res);
            echo "<br><br>";
            
            //Moving to the next element
            $xmlIterator->next();
            $res = $xmlIterator->getChildren();
            print_r($res);
         ?>      
      </body>
   </head>   
</html> 

这将产生以下结果:

SimpleXMLIterator Object ( 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
) 
SimpleXMLIterator Object ( 
   [Name] => CoffeeScript [Pages] => 235 
   [Author] => Kasyap [Version] => 2.5.1 
)

示例

在以下示例中,我们尝试检索 XML 文档的第三条记录:

Data.xml

<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
   
   <Tutorial>
      <Name>OpenCV</Name>
      <Pages>150</Pages>
      <Author>Maruti</Author>
      <Version>3.0</Version>
   </Tutorial>
</Tutorials>

sample.html

<html>
   <head>
      <body>
         <?php
            $doc = new DOMDocument;
            
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator("Data.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind();
            
            //Retrieving the iterator
            $xmlIterator->next();
            $xmlIterator->next();
            $res = $xmlIterator->getChildren();
            print_r($res);		
         ?>      
      </body>
   </head>   
</html> 

这将产生以下输出:

SimpleXMLIterator Object ( 
   [Name] => OpenCV [Pages] => 150 
   [Author] => Maruti [Version] => 3.0 
)

示例

在以下示例中,我们尝试显示 XML 文件的所有记录:

data.xml

<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
   
   <Tutorial>
      <Name>OpenCV</Name>
      <Pages>150</Pages>
      <Author>Maruti</Author>
      <Version>3.0</Version>
   </Tutorial>
</Tutorials>

Sample.html

<html>
   <head>
      <body>
         <?php
            $doc = new DOMDocument;
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator("data.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind();
            //Retrieving the iterator
            //$xmlIterator->next();
            for($xmlIterator->rewind(); $xmlIterator->valid(); 
            $xmlIterator->next() ){
               foreach($xmlIterator->getChildren() as $key => $val) {
                  print($key ."::". $val . "<br>");
               }
               echo "<br>";
            }		
         ?>      
      </body>
   </head>   
</html> 

这将产生以下结果:

Name::JavaFX
Pages::535
Author::Krishna
Version::11

Name::CoffeeScript
Pages::235
Author::Kasyap
Version::2.5.1

Name::OpenCV
Pages::150
Author::Maruti
Version::3.0
php_function_reference.htm
广告