• PHP Video Tutorials

PHP - XMLReader::lookupNamespace() 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::lookupNamespace() function of the XMLReader class accepts a string value representing the namespace prefix and looks up in scope namespace for the given prefix.

Syntax

XMLReader::lookupNamespace($prefix);

Parameters

Sr.No Parameter & Description
1

prefix(Mandatory)

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

Return Values

This function returns a boolean value which is TRUE incase of 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::lookupNamespace() 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('trail.xml');

   //reading the contents of the node
   $reader->read();
   $res = $reader->lookupNamespace("ns"); 
   print("Name space: ".$res);
   
   //Closing the reader
   $reader->close();
?>

This will produce following result −

Name space: testnamespace

Example

Following is another example of this function −

<?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 of the node
   $reader->read();
   $res = $reader->lookupNamespace("ns"); 
   print("Name space: ".$res);

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

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

This will produce following result −

Name space: testnamespace
php_function_reference.htm
Advertisements