PHP - XMLReader::read() 函式



定義和用法

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

XMLReader::read() 函式是 XMLReader 類的一個方法,它將游標移動到當前 XML 檔案的下一個節點。

語法

XMLReader::read();

引數

此函式不接受任何引數。

返回值

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

PHP 版本

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

示例

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

mydata.xml

<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
</Tutorials>

sample.php

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

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

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

   $data = $reader->readInnerXml();
   print($data);

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

這將產生以下結果:

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

<Tutorial>
   <Name>CoffeeScript</Name>
   <Pages>235</Pages>
   <Author>Kasyap</Author>
   <Version>2.5.1</Version>
</Tutorial>

示例

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

mydata.xml

<data> 
   <name>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 of XML document
   $reader->read(); 
   $reader->read();
   $reader->next("phone");

   //Reading the contents
   print($reader->name."\n");
   print($reader->readString());

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

這將產生以下結果:

phone
9848022338
php_function_reference.htm
廣告