PHP - XMLReader::isValid() 函式



定義和用法

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

XMLReader 類的XMLReader::isValid()函式用於驗證讀取/解析的文件的當前節點是否有效。

語法

XMLReader::isValid();

引數

此函式不接受任何引數。

返回值

此函式返回一個布林值,如果解析的文件有效則為 TRUE,無效則為 FALSE。

PHP 版本

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

示例

以下示例演示了XMLReader::isValid()函式的用法 −data.xml

<dataaa> 
   <name>Raju</name> 
   <age>32</age> 
   <phone>9848022338</phone> 
	<city>Hyderabad</city>
</data> 

sample.php

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

   //Opening a reader
   $reader->open("test.xml");

   //Setting the parser property
   //$xml->setParserProperty($reader->VALIDATE, true);
   $bool = $reader->isValid();
   
   if($bool){
      print("Current node is valid");
   } else {
      print("Current node is invalid");
   }

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

這將產生以下結果 −

Current node is invalid 
php_function_reference.htm
廣告