PHP - XMLReader::open() 函式



定義和用法

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

XMLReader::open() 函式是 XMLReader 類的一個函式,它接受一個字串值,該字串值表示要讀取其內容的 XML 文件的絕對路徑。

語法

XMLReader::open ($URI [$encoding, $options]);

引數

序號 引數和描述
1

URI(必填)

這是一個字串值,表示 XML 文件的路徑。

2

encoding(必填)

這是一個字串值,表示編碼或 Null。

3

options(可選)

這是一個整數值,表示位掩碼。

返回值

此函式返回一個布林值,成功時為 TRUE,失敗時為 FALSE。當您靜態呼叫此函式時,如果成功,它會返回一個 XMLReader 物件,失敗時返回 FALSE。

PHP 版本

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

示例

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

data.xml

<Data>
   <Employee>
      <Name>Krishna</Name>
      <Age>22</Age>
      <City>Hyderabad</City>   
   </Employee>

   <Employee>
      <Name>Raju</Name>
      <Age>30</Age>
      <City>Delhi</City>
   </Employee>
</Data>

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->next()){
      print($reader->readString());
   }
   
   //Closing the reader
   $reader->close();
?>

這將產生以下結果:

Krishna
22
Hyderabad

Raju
30
Delhi

示例

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

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

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

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

   $data = $reader->expand();
   print_r($data);

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

這將產生以下結果:

DOMElement Object (
   [tagName] => Tutorials
   [schemaTypeInfo] =>
   [nodeName] => Tutorials
   [nodeValue] =>

   JavaFX
   535
   Krishna
   11

   CoffeeScript
   235
   Kasyap
   2.5.1

   [nodeType] => 1
   [parentNode] =>
   [childNodes] => (object value omitted)
   [firstChild] => (object value omitted)
   [lastChild] => (object value omitted)
   [previousSibling] =>
   [nextSibling] =>
   [attributes] => (object value omitted)
   [namespaceURI] =>
   [prefix] =>
   [localName] => Tutorials
   [baseURI] =>
   [textContent] =>

   JavaFX
   535
   Krishna
   11

   CoffeeScript
   235
   Kasyap
   2.5.1
)
php_function_reference.htm
廣告