<?Php
require "xml-sample1.php";
$main1 = new SimpleXMLElement($str_xml);
$main1->addAttribute('type', 'student');
echo $main1->asXML();
?>
Output is here.
<?xml version="1.0" standalone="yes"?>
<details type="student">
<name>Abcd</name>
<address>
<door_no>234-ac</door_no>
<street>Great Wall</street>
<city>Alabama</city>
</address>
</details>
$main1->address->addAttribute('type', 'local');
<address type="local">
Now adding to another node
$main1->address->street->addAttribute('type', 'main');
<street type="main">Great Wall</street>
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.