<?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
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.