PHP | SimpleXMLElement::getName() Function

Pre-requisite: Read XML basics

The SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.

Syntax:



string SimpleXMLElement::getName( void )

Parameter: This function does not accept any parameter.

Return Value: It returns a string which represents the name of XML element of a SimpleXMLElement object.

Note: This function is available on PHP 5.1.3 and newer version.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <username>Geeks123 </username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Noide India
    </detail>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Display the name of element
echo "Base tag name: " . $xml->getName() . "<br>";
  
foreach($xml->children() as $child) {
    echo "child node: " . $child->getName() 
        . " = " . $child . "</br>";
}
  
?>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$user = <<<XML
<user>
    <username>Geeks123</username>
    <name>GeeksforGeeks</name>
    <phone>+91-XXXXXXXXXX</phone>
    <detail font-color="blue" font-size="24px">
        Computer science portal
    </detail>
    <address>
        <city>Noida</city>
        <country>India</country>
    </address>
</user>
XML;
  
// Loading string as simple xml object
$xml = simplexml_load_string($user);
  
// Recursive function called
getname_rec($xml, 0);
   
// The getname_rec() function definition 
function getname_rec($xml, $depth) {
      
    print_space($depth);
      
    echo "tag name: " . $xml->getName() . "<br>";
      
    foreach($xml->children() as $child) {
        if($child->count() > 0) {
              
            // If there exists any child of current node
            getname_rec($child, $depth+1);
        }
        else
              
            // If there is no child of the current node
            print_space($depth);
            echo " child node: " . $child->getName()
                    . " = " . $child . "</br>";
        }
    }
}
  
// Fucntion to print 3X$i number of spaces
function print_space($i) {
    for($x = 0; $x < $i*3; $x++) {
        echo " ";
    }
}
  
?>

chevron_right


Output:

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



My Personal Notes arrow_drop_up

I am a programmer by hobby

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.