getName(): retrieves the name of an XML tag from an XML element in an XML string or file

Get the current XML element name

getName() returns the element name represented by the current SimpleXMLElement object.

<?php
$xml = simplexml_load_string('<book><title>PHP</title></book>');
echo $xml->getName();
echo $xml->title->getName();
?>

Current example first; the detailed examples below are retained for additional learning and comparison.

We will use SimpleXMLElement object for this. Here is the sample string we will be using and we kept this sample xml string inside xml-sample1.php. We will load this string at the starting of the page.

xml-sample1.php

<?php
$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<details>
<name>Abcd</name>
<address>
 <door_no>234-ac</door_no>
 <street>Great Wall</street>
 <city>Alabama</city>
</address>

</details>
XML;
?>
Here is the PHP code using getName()
<?php
require "xml-sample1.php";
$main1 = new SimpleXMLElement($str_xml);

echo 'getName : '.$main1->getName();
?>
The output is
getName : details
The get the name of all balance tags we will be using for loop and the child function. Here is the code.
<?php
require "xml-sample1.php";
$main1 = new SimpleXMLElement($str_xml);

foreach ($main1 as $child1) {
echo  '<br>getName : '. $child1->getName();
foreach ($child1 as $child2) {
echo '<br>getName  : '. $child2->getName();
}
}
?>
Output is here
getName : name

getName : address

getName : door_no

getName : street

getName : city

XML XML Children



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer