Wednesday, July 3, 2024
HomeLanguagesPhpPHP | SimpleXMLElement getNamespaces() Function

PHP | SimpleXMLElement getNamespaces() Function

Pre-requisite: Read XML basics

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

Syntax:

array SimpleXMLElement::getNamespaces( $recursive )

Parameter: This function accepts single parameter $recursive which is optional. Its default value is FALSE. If True is passed then it returns namespaces in the parent as well as child node recursively. If set False then it only returns the namespaces of the parent 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::getNamespaces() function in PHP:

Program 1:




<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://neveropen.co.za/user">
<single_user id="1">
    <user_id:id>12345</user_id:id>
    <username>Geeks123</username>
    <name>neveropen</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->getNamespaces(1);
  
// Display output
print_r($result);
  
?>


Output:

Array
(
    [user_id] => http://neveropen.co.za/user
)

Program 2:




<?php
  
// Loading XML document to $user
$user = <<<XML
<user xmlns:user_id="http://neveropen.co.za/user">
    <single_user id="1" xmlns:name="http://neveropen.co.za/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://neveropen.co.za/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://neveropen.co.za/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->getNamespaces(TRUE);
   
// Displaying output
print_r($result);
  
?>


Output:

Array
(
    [user_id] => http://neveropen.co.za/user
    [name] => http://neveropen.co.za/user/name
)

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments