PHP | SimpleXMLElement getDocNamespaces() Function

Pre-requisite: Read XML Basics

The SimpleXMLElement::getDocNamespaces() function is an inbuilt function in PHP which is used to retrieve the namespaces declared in XML document.

Syntax:



array SimpleXMLElement::getDocNamespaces( $recursive, $from_root )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $recursive: It is optional and boolean parameter. Its default value is False. If True is passed as parameter then it returns namespaces in parent as well as child node recursively. If it set to False then it only returns the namespaces of the parent node.
  • $from_root: It is optional and boolean parameter. The default value is True. If it set to True then it will check namespaces from the root of the XML document. If it set to False then it will check namespaces under a child node.

Return Value: This function returns an array of namespace names with their associated URIs.

Note: This function is available for PHP 5.1.2 and newer version.

Below programs illustrate the SimpleXMLElement::getDocNamespaces() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://geeksforgeeks.org/user">
<single_user id="1">
    <user_id:id>12345</user_id:id>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noida India
    </detail>
</single_user>
  
<single_user id="2">
    <user_id:id>15980</user_id:id>
    <username>Geeks54321</username>
    <name>Geeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noida India
    </detail>
</single_user>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Retrieving namespaces
$result = $xml->getDocNamespaces();
  
// Display output
print_r($result);
  
?>

chevron_right


Output:

Array
(
    [user_id] => http://geeksforgeeks.org/user
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://geeksforgeeks.org/user">
    <single_user id="1" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>12345</user_id:id>
        <username>rakesh123</username>
        <name:firstname>Rakesh</name:firstname>
        <name:lastname>Kumar</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Noida India</detail>
    </single_user>
      
    <single_user id="2" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>57833</user_id:id>
        <username>man123</username>
        <name:firstname>Manjeet</name:firstname>
        <name:lastname>Singh</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Kolkata, India</detail>
    </single_user>
      
    <single_user id="3" xmlns:name="http://geeksforgeeks.org/user/name">
        <user_id:id>98944</user_id:id>
        <username>ak98</username>
        <name:firstname>Ak</name:firstname>
        <name:lastname>Singh</name:lastname>
        <phone>+91-XXXXXXXXXX</phone>
        <detail>Noida India</detail>
    </single_user>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Retrieving namespaces
$result = $xml->getDocNamespaces(TRUE);
   
// Displaying output
print_r($result);
  
?>

chevron_right


Output:

Array
(
    [user_id] => http://geeksforgeeks.org/user
    [name] => http://geeksforgeeks.org/user/name
)

Reference:https://www.php.net/manual/en/simplexmlelement.getdocnamespaces.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.