Open In App

PHP | SimpleXMLElement::getName() Function

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

php




<?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>";
}
 
?>


Output: 
 

Example 2: 
 

php




<?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>";
        }
    }
}
 
// Function to print 3X$i number of spaces
function print_space($i) {
    for($x = 0; $x < $i*3; $x++) {
        echo " ";
    }
}
 
?>


Output: 
 

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



Similar Reads

PHP | ReflectionParameter getName() Function
The ReflectionParameter::getName() function is an inbuilt function in PHP which is used to return the name of the specified parameter. Syntax: string ReflectionParameter::getName ( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the name of the specified parameter. Below programs illustrate the R
2 min read
PHP | Reflection getName() Function
The Reflection::getName() function is an inbuilt function in PHP which is used to return the name of the specified class. Syntax: string Reflection::getName( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the name of the specified class. Below programs illustrate the Reflection::getName() functi
1 min read
PHP | DateTimeZone::getName() Function
The DateTimeZone::getName() function is an inbuilt function in PHP which is used to return the name of the created timezone. Syntax: DateTimeZone::getName() Parameters: This function does not accept any parameter. Return Values:: This function return the name of the created timezone. Below programs illustrate the DateTimeZone::getName() function: P
1 min read
PHP | ReflectionExtension getName() Function
The ReflectionExtension::getName() function is an inbuilt function in PHP which is used to return the name of the specified extension. Syntax: string ReflectionExtension::getName( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the name of the specified extension. Below programs illustrate the Ref
1 min read
PHP | ReflectionProperty getName() Function
The ReflectionProperty::getName() function is an inbuilt function in PHP which is used to return the name of the specified property. Syntax: string ReflectionProperty::getName ( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the name of the specified property. Below programs illustrate the Refle
1 min read
PHP | SimpleXMLElement children() Function
Pre-requisite: Read XML BasicsThe SimpleXMLElement::children() function is an inbuilt function in PHP which returns children of a given node in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and described below: $namespace: It is op
3 min read
PHP | SimpleXMLElement __toString() Function
The XMLReader::__toString() function is an inbuilt function in PHP which is used to get the text content that is directly in current element. This function does not return text content that is inside this element's children. Syntax: void XMLReader::__toString( void ) Parameters: This function doesn’t accept any parameters.Return Value: This functio
1 min read
PHP | SimpleXMLElement saveXML() Function
The SimpleXMLElement::saveXML() function is an inbuilt function in PHP which return well-formed XML string from a SimpleXML object. It is an alias of SimpleXMLElement::asXML() function. Syntax: mixed SimpleXMLElement::saveXML( string $filename ) Parameter: This function accepts single parameter $filename which is optional. It specified this functio
2 min read
PHP | SimpleXMLElement count() Function
Pre-requisite: Read XML Basics The SimpleXMLElement::count() function is an inbuilt function in PHP which counts number of child element in a SimpleXML object. Syntax: int SimpleXMLElement::count() Parameter: This function does not accept any parameters. Return Value: This function returns number of children of an element. Note: This function is av
2 min read
PHP | SimpleXMLElement::__construct() Function
Pre-requisite:XML The __construct() function is an inbuilt function in PHP that is used to create a new SimpleXMLElement object for XML. Syntax: SimpleXMLElement::__construct( $data, $options, $data_is_url, $namespace, $is_prefix ) Parameters: This function accepts five parameters as mentioned above and described below: $data: It is a required para
3 min read