PHP - XMLReader::setParserProperty() 函式



定義和用法

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

XMLReader 類的 XMLReader::setParserProperty() 函式接受一個表示屬性(解析器選項)的整數值和一個布林值作為引數,並將指定的選項/屬性設定為當前讀取器。

語法

XMLReader::setParserProperty($property, $value);

引數

序號 引數及描述
1

property(必填)

這是一個整數值,表示您需要設定的屬性/選項。它可以是以下之一:

  • XMLReader::LOADDTD

  • XMLReader::DEFAULTATTRS

  • XMLReader::VALIDATE

  • XMLReader::SUBST_ENTITIES

2

value(必填)

這是一個布林值,您可以透過將 TRUE 作為值傳遞給此引數來將指定的選項設定為讀取器。

返回值

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

PHP 版本

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

示例

以下示例演示了

XMLReader::setParserProperty()

函式的用法:

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

   //Setting the parser property 
   $reader->setParserProperty(XMLReader::VALIDATE, true); 

   $bool = $reader->getParserProperty(XMLReader::VALIDATE); 
      
   if ($bool) { 
      print("Property is set"); 
   }

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

這將產生以下結果:

Property is set 

示例

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

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

   //Setting the parser property 
   $reader->setParserProperty(XMLReader::SUBST_ENTITIES, true); 
   $reader->setParserProperty(XMLReader::LOADDTD, true); 
   $reader->setParserProperty(XMLReader::DEFAULTATTRS, true); 
   $reader->setParserProperty(XMLReader::VALIDATE, true); 

   $bool1 = $reader->getParserProperty(XMLReader::SUBST_ENTITIES); 
   if ($bool1) { 
       print("The SUBST_ENTITIES Property is set \n"); 
   } 
   
   $bool1 = $reader->getParserProperty(XMLReader::LOADDTD); 
   if ($bool1) { 
       print("The LOADDTD Property is set \n"); 
   } $bool1 = $reader->getParserProperty(XMLReader::DEFAULTATTRS); 
   
   if ($bool1) { 
       print("The DEFAULTATTRS Property is set \n"); 
   } $bool1 = $reader->getParserProperty(XMLReader::VALIDATE); 
   
   if ($bool1) { 
       print("The VALIDATE Property is set"); 
   } 
   
   //Closing the reader
   $reader->close();
?> 

這將產生以下結果:

The SUBST_ENTITIES Property is set
The LOADDTD Property is set
The DEFAULTATTRS Property is set
The VALIDATE Property is set
php_function_reference.htm
廣告