PHP - XMLReader::getParserProperty() 函式



定義和用法

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

XMLReader::getParserProperty() 函式是 XMLReader 類的一個函式,它接收一個表示屬性(解析器選項)的整數值作為引數,如果指定的屬性在當前 XML 閱讀器上設定,則返回 TRUE。

語法

XMLReader::getParserProperty($property);

引數

序號 引數及描述
1

property(必填)

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

  • XMLReader::LOADDTD

  • XMLReader::DEFAULTATTRS

  • XMLReader::VALIDATE

  • XMLReader::SUBST_ENTITIES

返回值

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

PHP 版本

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

示例

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

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
廣告