PHP - XMLReader::XML() 函式



定義和用法

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

XMLReader::XML() 函式是 XMLReader 類的一個函式,它接受一個字串值作為引數,該字串值表示 XML 文件的內容,並對其進行讀取/解析。

語法

XMLReader::xml($data [$encoding, $options]);

引數

序號 引數及描述
1

data(必填)

這是一個字串值,表示 XML 文件的內容。

2

encoding(必填)

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

3

options(可選)

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

返回值

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

PHP 版本

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

示例

以下示例演示了 XMLReader::XML() 函式的使用方法:

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

   $data = "<Data>
      <Employee>
         <Name>Krishna</Name>
         <Age>22</Age>
         <City>Hyderabad</City>   
      </Employee>

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

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

   //Reading the contents of the XML file
   while($reader->next()){
      print($reader->readString());
   }
   
   //Closing the reader
   $reader->close();
?>

這將產生以下結果:

Krishna
22
Hyderabad

Raju
30
Delhi

示例

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

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

   $data = "<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>";

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

   //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>

示例

以下是帶有可選引數的此函式的示例:

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

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

   //Opening a reader
   $reader->xml($data, "UTF-8");

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

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

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

這將產生以下結果:

DOMElement Object (
   [tagName] => data
   [schemaTypeInfo] =>
   [nodeName] => data
   [nodeValue] =>
   Raju
   32
   9848022338
   Hyderabad

   [nodeType] => 1
   [parentNode] =>
   [childNodes] => (object value omitted)
   [firstChild] => (object value omitted)
   [lastChild] => (object value omitted)
   [previousSibling] =>
   [nextSibling] =>
   [attributes] => (object value omitted)
   [namespaceURI] =>
   [prefix] =>
   [localName] => data
   [baseURI] =>
   [textContent] =>
   Raju
   32
   9848022338
   Hyderabad
)
php_function_reference.htm
廣告