PHP - XMLReader::moveToElement() 函式



定義和用法

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

XMLReader::moveToElement() 函式是 XMLReader 類的一個函式,它將游標移動到 XML 文件中當前屬性的父元素。

語法

XMLReader::moveToElement();

引數

此函式不接受任何引數。

返回值

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

PHP 版本

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

示例

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

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("trail.xml");

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

這將產生以下結果:

Employee
Name
Age
City
Phone

示例

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

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("trail.xml");

   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttribute("att");

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

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

這將產生以下結果:

near

示例

<?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");

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

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

這將產生以下結果:

test_attribute
php_function_reference.htm
廣告