Add an attribute to the root element and to the body element in PHP

Description

The following code shows how to add an attribute to the root element and to the body element.

Example


//from w w w .ja va2s .c o m
<?php
   $note=<<<XML
       <note>
       <to>Work</to>
       <from>Home</from>
       <heading>Reminder</heading>
       <body>This is a message.</body>
       </note>
XML;

    $xml=new SimpleXMLElement($note);
    $xml->addAttribute("type","private");
    $xml->body->addAttribute("date","2013-01-01");

    echo $xml->asXML();
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser