• PHP Video Tutorials

PHP - SimpleXMLElement::getDocNamespaces() 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::getDocNamespaces() function retrieves and returns the namespaces declared in a document.

Syntax

SimpleXMLElement::getDocNamespaces([$recursive, $from_root]);

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.

2

from_root (Optional)

This is a boolean value if passed TRUE this function checks namespaces under a child node (instead of root node).

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

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0' standalone='yes'?>
            <Tutorial xmlns:p='http://test.org/ns'>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            $result = $xml->getDocNamespaces();
            print_r($result);	  
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

JavaFX 535 Krishna 11 600

SimpleXMLElement Object ( 
   [@attributes] => Array ( [type] => test ) 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
   [Tutorial] => SimpleXMLElement Object 
   ( [Price] => 600 ) 
)

Example

Following is an example of this function with optional parameters −

<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->getDocNamespaces(TRUE, TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html>

This will produce the following output −

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