addAttribute() to XML object

We can add attributes to different elements of XML object by using addAttribute() function. We will try with different examples to add attributes at different level of child node. We used asXML function to display XML string. We have used our sample xml file for this. Here is the code.


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">

  <street>Great Wall</street>


Adding attribute to different level of XML node.

We will only show the changed line here and the respective output
$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>
Your Rating

Post your comments , suggestion , error , requirements etc here .

HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 All rights reserved worldwide Privacy Policy Disclaimer