• PHP Video Tutorials

PHP XMLReader::close() 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::close() function of the XMLReader class is used to close the current object that is being parsed.

Syntax

XMLReader::close();

Parameters

This function does not accept any parameters.

Return Values

This function returns a boolean value which is TRUE in case of success and FALSE in case of 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::close() 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");

   //reading the contents of the XML file
   while($reader->next()){
      print($reader->readString());
   }
   //Closing the reader
   $reader->close();
?>

This will produce following result −

Krishna
22
Hyderabad

Raju
30
Delhi

Example

Following is another example of this function −

mydata.xml

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

   //Reading the contents
   $reader->read();

   $data = $reader->expand();
   print_r($data);

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

This will produce following result −

DOMElement Object (
   [tagName] => data
   [schemaTypeInfo] =>
   [nodeName] => data
   [nodeValue] =>
   Raju
   32
   9848022338
   Hyderabad

   [nodeType] => 1
   [parentNode] =>
   [childNodes] => (object value omitted)
   [firstChild] => (object value omitted)
   [lastChild] => (object value omitted)
   [previousSibling] =>
   [nextSibling] =>
   [attributes] => (object value omitted)
   [namespaceURI] =>
   [prefix] =>
   [localName] => data
   [baseURI] =>
   [textContent] =>
   Raju
   32
   9848022338
   Hyderabad
)
php_function_reference.htm
Advertisements