The SimpleXMLElement::getDocNamespaces() function is a built-in function in PHP that is used to retrieve namespaces declared in an XML document.
Syntax: array SimpleXMLElement::getDocNamespaces ($recursive, $from_root)
Parameters:This function takes two parameters as above and described below:
- $recursive:This is an optional and boolean parameter. The default is False. If True is passed as a parameter, then it recursively returns the namespaces in the parent as well as the child node. If set to False, then it only returns the namespaces of the parent node.
- $from_root:This is an optional and boolean parameter. The default is correct. If set to True, it will validate namespaces from the root of the XML document. If set to False, it will check the namespaces under the child node.
Return Value:This function returns an array of namespace names with their corresponding URIs.
Note.This function is available for PHP 5.1.2 and later.The following programs illustrate the SimpleXMLElement::getDocNamespaces() function in PHP:
Program 1:
// Load the XML document into $user
$user
= < < < XML
< user xmlns: user_id =
" http://engineerforengineer.org/user "
>
< single_user id =
"1"
>
< user_id: id > 12345 < / user_id: id >
< username > Geeks123 < / username >
< name > GeeksforGeeks < / name >
+ 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 >
+ 91-XXXXXXXXXX < / phone >
< detail font-color =
"blue"
font-size =
"24px"
>
Noida India
< / detail >
< / single_user >
< / user >
XML;
// Load the string as a simple xml object
$xml
= simplexml_load_string (
$user
);
// Retrieving namespaces
$result
=
$xml
-> getDocNamespaces();
// Show output
print_r (
$result
);
?>
Exit:Array ([ user_id] = > http://engineerforengineer.org/user)
Program 2:
// Load XML document into $user
$user
= < < < XML
< user xmlns: user_id =
" http://engineerforengineer.org/user "
>
< single_user id =
" 1 "
xmlns: name =
" http://engineerforengineer.org/user/name "
>
< user_id: id > 12345 < / user_id: id >
< username > rakesh123 < / username >
< name: firstname > Rakesh < / name: firstname >
< name: lastname > Kumar < / name: lastname >
+ 91-XXXXXXXXXX < / phone >
< detail > Noida India < / detail >
< / single_user >
< single_user id =
" 2 "
xmlns: name =
" http://engineerforengineer.org/user/name "
>
< user_id: id > 57833 < / user_id: id >
< username > man123 < / username >
< name: firstname > Manjeet < / name: firstname >
< name: lastname > Singh < / name: lastname >
+ 91-XXXXXXXXXX < / phone >
< detail > Kolkata, India < / detail >
< / single_user >
< single_user id =
" 3 "
xmlns: name =
" http://engineerforengineer.org/user/name "
>
< user_id: id > 98944 < / user_id: id >
< username > ak98 < / username >
< name: firstname > Ak < / name: firstname >
< name: lastname > Singh < / name: lastname >
+ 91-XXXXXXXXXX < / phone >
< detail > Noida India < / detail >
< / single_user >
< / user >
XML;
// Load the string as a simple xml object
$xml
= simplexml_load_string (
$user
);
// Retrieving namespaces
$result
=
$xml
-> getDocNamespaces (TRUE);
// Display the output
print_r (
$result
);
?>
Exit:Array ([ user_id] = > http://engineerforengineer.org/user [name] = > http://engineerforengineer.org/user/name)
Link: https://www.php.net/manual/en/simplexmlelement.getdocnamespaces.php