• PHP Video Tutorials

PHP - XMLReader::setParserProperty() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. XMLReader extension is used to read/retrieve the contents of an XML document i.e. using the methods of the XMLReader class you can read each node of an XML document.

The XMLReader::setParserProperty() function of the XMLReader class accepts an integer value representing a property (parser option) and a boolean value as parameters and sets the specified option/property to the current reader.

Syntax

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

Parameters

Sr.No Parameter & Description
1

property(Mandatory)

This is an integer value representing the property/option you need to set. It can be one of the following −

  • XMLReader::LOADDTD

  • XMLReader::DEFAULTATTRS

  • XMLReader::VALIDATE

  • XMLReader::SUBST_ENTITIES

2

value(Mandatory)

This is a boolean value you can set the specified option to the reader by passing TRUE as value to this parameter.

Return Values

This function returns a boolean value which is TRUE in case of success and FALSE in case of failure. When you call this function statically, it returns an XMLReader object incase of success and FALSE on failure.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the

XMLReader::setParserProperty()

function −

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

This will produce following result −

Property is set 

Example

Following is another example of this function −

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

This will produce following result −

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
Advertisements