Add a child element to the body element and a footer element in PHP

Description

The following code shows how to add a child element to the body element and a footer element.

Example


/* ww w  .jav a2 s  .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);

    // Add a child element to the body element
    $xml->body->addChild("date","2013-01-01");

    // Add a child element after the last element inside note
    $footer=$xml->addChild("footer","Some footer text");

    echo $xml->asXML();
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser