PHP - SimpleXMLIterator::rewind() 函式



定義和用法

XML 是一種標記語言,用於在網路上共享資料,XML 可供人和機器讀取。SimpleXMLIterator 用於迭代 XML 文件的所有節點。

SimpleXMLIterator::rewind() 函式將迭代器移動到起始元素。

語法

SimpleXMLIterator::rewind(void);

引數

此函式不接受任何引數。

返回值

此函式不返回任何值。

PHP 版本

此函式首次在 PHP 5 版本中引入,並在所有後續版本中均有效。

示例

以下示例演示了 **SimpleXMLIterator::rewind()** 函式的用法。

<html>
   <head>
      <body>
         <?php
            $doc = new DOMDocument;
            $data = "<Data><Name>Krishna</Name><Age>30</Age></Data>";
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator($data);
            //Rewinding the iterator
            $xmlIterator->rewind(); 
            print_r($xmlIterator->current());		
         ?>      
      </body>
   </head>   
</html> 

這將產生以下結果:

SimpleXMLIterator Object ( [0] => Krishna )

示例

在以下示例中,我們嘗試使用 current() 和 next() 函式檢索 XML 檔案的所有元素:

<html>
   <head>
      <body>
         <?php
            $doc=new DOMDocument;
            $data="<?xml version='1.0' encoding='UTF-8'?>
            <Employee>
               <Name>Raju</Name>
               <Age>25</Age>
               <Salary>2000</Salary>
            </Employee>";
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator($data);
            $xmlIterator->rewind(); 
            print_r($xmlIterator->current()); 
            echo "<br><br>";
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());	
            echo "<br><br>";
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());		
         ?>      
      </body>
   </head>   
</html> 

這將產生以下輸出:

SimpleXMLIterator Object ( [0] => Raju )
SimpleXMLIterator Object ( [0] => 25 )
SimpleXMLIterator Object ( [0] => 2000 )

示例

在下面,我們嘗試列印 XML 檔案的元素一次,然後再次列印前兩個元素:

data.xml

<Tutorial>
   <Name>JavaFX</Name>
   <Pages>535</Pages>
   <Author>Krishna</Author>
   <Version>11</Version>
</Tutorial>

Sample.xml

<html>
   <head>
      <body>
         <?php
            $doc = new DOMDocument;
            //Creating the SimpleXMLIterator
            $xmlIterator = new SimpleXMLIterator("data.xml", 0, TRUE, "", FALSE);
            $xmlIterator->rewind(); 
            print_r($xmlIterator->current()); 
            echo "<br><br>";
            
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());	
            echo "<br><br>";
            
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());		
            echo "<br><br>";
            
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());
            echo "<br><br>";
            
            print("Printing first two elements ... (again) <br><br>");
            $xmlIterator->rewind();
            print_r($xmlIterator->current()); 
            echo "<br><br>";
            
            print_r($xmlIterator->next()); 
            print_r($xmlIterator->current());
         ?>      
      </body>
   </head>   
</html> 

這將產生以下結果:

SimpleXMLIterator Object ( [0] => JavaFX )
SimpleXMLIterator Object ( [0] => 535 )
SimpleXMLIterator Object ( [0] => Krishna )
SimpleXMLIterator Object ( [0] => 11 )
Printing first two elements ... (again)
SimpleXMLIterator Object ( [0] => JavaFX )
SimpleXMLIterator Object ( [0] => 535 )
php_function_reference.htm
廣告