• PHP Video Tutorials

PHP SimpleXMLElement::getName() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The SimpleXMLElement class represents an XML document in PHP.

The SimpleXMLElement::getName() function retrieves and returns the name of the current XML element.

Syntax

SimpleXMLElement::getName();

Parameters

This function doesn’t accept any parameters.

Return Values

This function returns a string value representing the name of the current XML element.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the SimpleXMLElement::getName() function.

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name type = 'programming'>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            print("Name of the current element: ".$xml->getName());
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

Name of the current element: Tutorial

Example

Following example reads the contents of an XML file and prints the names of the elements in it −

data.xml

<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
   
   <Tutorial>
      <Name>OpenCV</Name>
      <Pages>150</Pages>
      <Author>Maruti</Author>
      <Version>3.0</Version>
   </Tutorial>
</Tutorials>

Sample.php:

<html>
   <head>      
      <body>         
         <?php
            $doc = new DOMDocument;
            $xml = simplexml_load_file("data.xml");
            //file to SimpleXMLElement 
            $xml = simplexml_import_dom($xml);
		
            print($xml->getName()."<br>");
               foreach ($xml->children() as $child){
               print("::". $child->getName() ."<br>");
               foreach ($child->children() as $child){
                  print(":::::". $child->getName() ."<br>");			   
               }
            }
         ?>
      </body>
   </head>
</html>

This will produce the following output −

Tutorials
::Tutorial
:::::Name
:::::Pages
:::::Author
:::::Version
::Tutorial
:::::Name
:::::Pages
:::::Author
:::::Version
::Tutorial
:::::Name
:::::Pages
:::::Author
:::::Version

Example

Following is another example of this function −

<html>
   <head>      
      <body>         
         <?php
            $data = "<Tutorials>  </Tutorials>";
            $xml = simplexml_load_string($data);
            print_r($xml);

            //Adding the child node
            $child = $xml->addChild('Tutorial');
            $child->addChild('Name', 'OpenCV');
            $child->addChild('Pages', '230');
            $child->addChild('Author', 'Maruthi');
            $child->addChild('Version', '5.5');
 
            print($xml->getName()."<br>");
            foreach ($xml->children() as $child){
               print("::". $child->getName() ."<br>");
               foreach ($child->children() as $child){
                  print(":::::". $child->getName() ."<br>");
               }
            }
         ?>
      </body>
   </head>
</html>

This will produce following result −

SimpleXMLElement Object ( ) Tutorials
::Tutorial
:::::Name
:::::Pages
:::::Author
:::::Version
php_function_reference.htm
Advertisements