• PHP Video Tutorials

PHP - XMLReader::moveToNextAttribute() 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::moveToNextAttribute() function of the XMLReader class moves the cursor to the next attribute in the XML document.

Syntax

XMLReader::moveToAttribute();

Parameters

This function does not accept any parameters.

Return Values

This function returns a boolean value which is TRUE on success and FALSE incase 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::moveToNextAttribute() function −

data.xml

<Employee>
   <Name id1 = "attr_name">Krishna</Name>
   <Age id2 = "attr_age">22</Age>
   <City id3 = "attr_city">Hyderabad</City>   
   <Phone id4 = "attr_phone">980000000</Phone>   
</Employee>

sample.php

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

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

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

   if ($reader->nodeType == XMLREADER::ELEMENT) { 
      $reader->moveToFirstAttribute(); 
      print($reader->name."\n");
      
      $reader->moveToNextAttribute(); 
      print($reader->name."\n");
   }
   
   //Closing the reader
   $reader->close();
?>

This will produce following result −

name_attr1
name_attr2

Example

Following is another example of this function −

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

   $data = "<Employee>
      <Name name_attr1 = 'n_val1' name_attr2 = 'n_val2'>Krishna</Name>
      <Age>22</Age>
      <City>Hyderabad</City>   
      <Phone>980000000</Phone>   
   </Employee>";

   //Opening a reader
   $reader->xml($data);

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

   $reader->moveToFirstAttribute(); 
   print($reader->name."\n");
      
   $reader->moveToNextAttribute(); 
   print($reader->name."\n");

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

This will produce following result −

name_attr1
name_attr2
php_function_reference.htm
Advertisements