PHP - SimpleXMLIterator::hasChildren() 函数



定义和用法

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

SimpleXMLIterator::hasChildren() 函数用于验证迭代器的当前元素是否包含子元素。

语法

SimpleXMLIterator::hasChildren(void);

参数

此函数不接受任何参数。

返回值

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

PHP 版本

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

示例

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

<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();
            if($xmlIterator->hasChildren()) {
               //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();
            if($xmlIterator->hasChildren()) {               
               //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("trail.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind();
            //Retrieving the iterator
            //$xmlIterator->next();
             for($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ){
               if($xmlIterator->hasChildren()) {
               $res = $xmlIterator->current();
                  print($res->Name ."<br>");
                  print($res->Pages ."<br>");
                  print($res->Author ."<br>");
                  print($res->Version ."<br>");
                  print("<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
广告