PHP - Fonction XMLReader :: getParserProperty ()

Définition et utilisation

XML est un langage de balisage permettant de partager les données sur le Web, XML est à la fois lisible par l'homme et par machine. L'extension XMLReader est utilisée pour lire / récupérer le contenu d'un document XML, c'est-à-dire en utilisant les méthodes de la classe XMLReader, vous pouvez lire chaque nœud d'un document XML.

le XMLReader::getParserProperty() La fonction de la classe XMLReader accepte une valeur entière représentant une propriété (option parser) en tant que paramètre et renvoie TRUE si la propriété spécifiée est définie sur le lecteur XML actuel.

Syntaxe

XMLReader::getParserProperty($property);

Paramètres

Sr. Non Paramètre et description
1

property(Mandatory)

Il s'agit d'une valeur entière représentant la propriété / option que vous devez définir. Cela peut être l'un des suivants -

  • XMLReader::LOADDTD

  • XMLReader::DEFAULTATTRS

  • XMLReader::VALIDATE

  • XMLReader::SUBST_ENTITIES

Valeurs de retour

Cette fonction renvoie une valeur booléenne qui est TRUE en cas de succès et FALSE en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation du XMLReader::getParserProperty() fonction -

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

Cela produira le résultat suivant -

Property is set

Exemple

Voici un autre exemple de cette fonction -

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

Cela produira le résultat suivant -

The SUBST_ENTITIES Property is set
The LOADDTD Property is set
The DEFAULTATTRS Property is set
The VALIDATE Property is set