• PHP Video Tutorials

PHP - XMLReader::moveToAttributeNs() 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::moveToAttributeNs() function of the XMLReader class accepts two string values representing the name of an attribute and the name space URI and moves the cursor to the specified attribute.

Syntax

XMLReader::moveToAttributeNs($name, $URI);

Parameters

Sr.No Parameter & Description
1

name(Mandatory)

This is a string value representing the name of an attribute.

2

URI(Mandatory)

This is a string value representing the namespace URI.

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::moveToAttributeNs() function −

data.xml

<?xml version="1.0" encoding="utf-8"?> 
<Employee xmlns:ns="testnamespace">
   <ns:Name ns:id = "name">Krishna</ns:Name>
   <ns:Age ns:id = "age">22</ns:Age>
   <ns:City ns:id = "city">Hyderabad</ns:City>   
   <ns:Phone ns:id = "phone">980000000</ns:Phone>   
</Employee>

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->read()){
      if ($reader->nodeType == XMLREADER::ELEMENT) { 
         $reader->moveToAttributeNs('id', 'testnamespace'); 
         print($reader->value."\n");
      }
   }
   //Closing the reader
   $reader->close();
?>

This will produce following result −

name
age
city
phone

Example

Following is another example of this function −

test.xml

<data xmlns:ns="testnamespace"> 
   <ns:name ns:att = "test_attribute">Raju</ns: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("trail.xml");

   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttributeNs("att", "testnamespace");
   print($reader->value."\n");

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

This will produce following result −

test_attribute

Example

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

   $data = "<data xmlns:ns = 'testnamespace'> 
      <ns:name ns:att = 'test_attribute'>Raju</ns:name> 
      <age>32</age> 
      <phone>9848022338</phone> 
      <city>Hyderabad</city>
   </data>";

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

   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttributeNs("att", "testnamespace");
   print($reader->value."\n");
   
   //Closing the reader
   $reader->close();
?>

This will produce following result −

test_attribute
php_function_reference.htm
Advertisements