Function SimpleXMLElement::getNamespaces() - it is a built-in function in PHP that is used to extract namespaces declared in an XML document.
Syntax:array SimpleXMLElement::getNamespaces ($recursive)
Parameter:This function takes one parameter
$recursive,which is optional. Its default value is - FALSE. If True is passed, it returns a recursive namespace in the parent and child nodes. If set to False, then it only returns the namespaces of the parent 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::getNamespaces() function in PHP:
Program 1:
// Load 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
-> getNamespaces (1);
// 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
-> getNamespaces (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.getnamespaces.php