PHP - XMLReader::moveToFirstAttribute() 函式



定義和用法

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

XMLReader 類的XMLReader::moveToFirstAttribute()函式將游標移動到 XML 文件中的第一個屬性。

語法

XMLReader::moveToFirstAttribute();

引數

此函式不接受任何引數。

返回值

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

PHP 版本

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

示例

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

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) { 
      $res = $reader->moveToAttribute('id4'); 
      $reader->moveToElement(); 
      print($reader->name."\n");

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

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

這將產生以下結果:

Name
id1

示例

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

<?php
   //Creating an XMLReader
   $reader = new XMLReader();
   $data= "<data> 
      <name att1 = 'test_attribute1'>Raju</name> 
      <age>32</age> 
      <phone>9848022338</phone> 
      <city att2 = 'test_attribute2'>Hyderabad</city>
   </data> 

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

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

   if ($reader->nodeType == XMLREADER::ELEMENT) { 
      $res = $reader->moveToAttribute('test_attribute2'); 
      print($reader->name."\n");

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

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

這將產生以下結果:

name
att1
php_function_reference.htm
廣告