• PHP Video Tutorials

PHP - SimpleXMLElement::getNameSpaces() 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. The SimpleXMLElement class represents an XML document in PHP.

The SimpleXMLElement::getNamespaces() function retrieves and returns the namespaces used in a document.

Syntax

SimpleXMLElement::getNamespaces([$recursive]);

Parameters

Sr.No Parameter & Description
1

recursive (Optional)

This is a boolean value if passed TRUE this function returns namespaces of parent and child nodes.

Return Values

This function returns an array containing the name spaces.

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 SimpleXMLElement::getNamespaces() function.

<html>
   <head>
      <body>
         <?php
            $str="<Tutorial xmlns:t='http://example.org/ns' xmlns:test='http://demo.com/test'>
               <t:Name test:ns='a'>JavaFX</t:Name>
               <t:Pages test:ns='b'>535</t:Pages>
               <t:Author test:ns='c'>Krishna</t:Author>
               <t:Version test:ns='d'>11</t:Version>
            </Tutorial>"; 
            $xml = new SimpleXMLElement($str);
            $result = $xml->getNamespaces(TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

array(2) { ["t"]=> string(21) "http://example.org/ns" ["test"]=> string(20) "http://demo.com/test" }

Example

Following is an example of this function −

<html>
   <head>
      <body>
         <?php
            $str="<Employee xmlns:contact='http://example.org/ns'>
               <Name>Ramu</Name>
               <Age>25</Age>
               <contact:City>Hyderabad</contact:City>
               <contact:Phone>9848022338</contact:Phone>
               <contact:email>test@gmail.com</contact:email>
            </Employee>"; 
            $xml = new SimpleXMLElement($str);
            $result = $xml->getNamespaces(TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html> 

This will produce the following output −

array(1) { ["contact"]=> string(21) "http://example.org/ns" }
php_function_reference.htm
Advertisements