PHP - XMLReader::moveToAttribute() 函式



定義和用法

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

XMLReader 類的XMLReader::moveToAttribute()函式接收一個表示屬性名稱的字串值,並將游標移動到指定的屬性。

語法

XMLReader::moveToAttribute($name);

引數

序號 引數 & 說明
1

name(必填)

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

返回值

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

PHP 版本

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

示例

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

data.xml

<Employee>
   <Name id = "name">Krishna</Name>
   <Age id = "age">22</Age>
   <City id = "city">Hyderabad</City>   
   <Phone id = "phone">980000000</Phone>   
</Employee>

sample.php

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

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

   //Reading the contents of the XML file
   while($reader->read()){
      if ($reader->nodeType == XMLREADER::ELEMENT) { 
         $reader->moveToAttribute('id2'); 
         print($reader->value."\n");
      }
   }
   //Closing the reader
   $reader->close();
?>

這將產生以下結果:

age

示例

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

test.xml

<data> 
   <name att = "test_attribute">Raju</name> 
   <age>32</age> 
   <phone>9848022338</phone> 
	<city>Hyderabad</city>
</data> 

sample.php

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

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

   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttribute("att");
   print($reader->value."\n");
   
   //Closing the reader
   $reader->close();
?> 

這將產生以下結果:

test_attribute

示例

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

   $data = "<data> 
      <name att = 'test_attribute'>Raju</name> 
      <age>32</age> 
      <phone>9848022338</phone> 
      <city>Hyderabad</city>
   </data> ";

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

   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttribute("att");
   print($reader->value."\n");
   
   //Closing the reader
   $reader->close();
?> 

這將產生以下結果:

test_attribute
php_function_reference.htm
廣告