當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP XMLReader getParserProperty()用法及代碼示例

XMLReader::getParserProperty()函數是PHP中的內置函數,用於檢查是否設置了指定的屬性。

用法:

bool XMLReader::getParserProperty( int $property )

參數:該函數接受單個參數$property,該參數保存一個與PARSER Options常量之一相對應的整數。解析器選項常量列表如下:



  • XMLReader::LOADDTD(1)這將加載DTD,但不會驗證。
  • XMLReader::DEFAULTATTRS(2)這將加載DTD和默認屬性,但不會驗證。
  • XMLReader::VALIDATE(3)這將加載DTD並在解析時進行驗證。
  • XMLReader::SUBST_ENTITIES(4)這將替代實體並擴展引用。

返回值:如果成功,此函數將返回TRUE,否則將返回FALSE。

異常:該函數在錯誤時引發XMLReaderException。

以下示例說明了PHP中的XMLReader::getParserProperty()函數:

範例1:

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div xmlns:x="geeksforgeeks"> 
        <x:h1 x:attrib="value"> 
          Namespaced Text  
        </x:h1> 
    </div>
  • index.php
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file with sample XML 
    $XMLReader->open('data.xml'); 
      
    // Check if XMLReader::VALIDATE is set or not 
    $isProperty = $XMLReader-> 
    getParserProperty(XMLReader::VALIDATE); 
      
    if (!$isProperty) { 
        echo 'Property isn\'t set.'; 
    } 
    ?>
  • 輸出:
    Property isn't set.

範例2:

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div xmlns:x="geeksforgeeks"> 
        <x:h1 x:attrib="value"> 
          Namespaced Text  
        </x:h1> 
    </div>
  • index.php
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file with sample XML 
    $XMLReader->open('data.xml'); 
      
    // Set the Parser Property 
    $XMLReader->setParserProperty( 
        XMLReader::VALIDATE, true); 
      
    // Check if XMLReader::VALIDATE 
    // is set or not 
    $isProperty = $XMLReader->getParserProperty( 
            XMLReader::VALIDATE); 
       
    if ($isProperty) { 
        echo 'Property is set.'; 
    } 
    ?>
  • 輸出:
    Property is set.

參考:https://www.php.net/manual/en/xmlreader.getparserproperty.php




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader getParserProperty() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。