PHP - XMLReader::moveToNextAttribute() 函式



定義和用法

XML 是一種用於在網路上共享資料的標記語言,XML 既可供人類閱讀,也可供機器讀取。XMLReader 擴充套件用於讀取/檢索 XML 文件的內容,即使用 XMLReader 類的 方法可以讀取 XML 文件的每個節點。

XMLReader::moveToNextAttribute() 函式是 XMLReader 類中的一個函式,用於將游標移動到 XML 文件中的下一個屬性。

語法

XMLReader::moveToAttribute();

引數

此函式不接受任何引數。

返回值

此函式返回一個布林值,成功時返回 TRUE,失敗時返回 FALSE。

PHP 版本

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

示例

以下示例演示了 XMLReader::moveToNextAttribute() 函式的用法:

data.xml

<Employee>
   <Name id1 = "attr_name">Krishna</Name>
   <Age id2 = "attr_age">22</Age>
   <City id3 = "attr_city">Hyderabad</City>   
   <Phone id4 = "attr_phone">980000000</Phone>   
</Employee>

sample.php

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   //Opening a reader
   $reader->open("trail.xml");

   //Reading the contents of the XML file
   $reader->read();
   $reader->read();
   $reader->read();

   if ($reader->nodeType == XMLREADER::ELEMENT) { 
      $reader->moveToFirstAttribute(); 
      print($reader->name."\n");
      
      $reader->moveToNextAttribute(); 
      print($reader->name."\n");
   }
   
   //Closing the reader
   $reader->close();
?>

這將產生以下結果:

name_attr1
name_attr2

示例

以下是此函式的另一個示例:

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   $data = "<Employee>
      <Name name_attr1 = 'n_val1' name_attr2 = 'n_val2'>Krishna</Name>
      <Age>22</Age>
      <City>Hyderabad</City>   
      <Phone>980000000</Phone>   
   </Employee>";

   //Opening a reader
   $reader->xml($data);

   //Reading the contents of the XML file
   $reader->read();
   $reader->read();
   $reader->read();

   $reader->moveToFirstAttribute(); 
   print($reader->name."\n");
      
   $reader->moveToNextAttribute(); 
   print($reader->name."\n");

   //Closing the reader
   $reader->close();
?>

這將產生以下結果:

name_attr1
name_attr2
php_function_reference.htm
廣告