The addChild() function adds a child element to the SimpleXML element.
PHP addChild() Function has the following syntax.
addChild(name,value,ns);
Parameter | Is Required | Description |
---|---|---|
name | Required. | Name of the child element to add |
value | Optional. | Value of the child element |
ns | Optional. | A namespace for the child element |
Returns a SimpleXMLElement object that represents the child added to the XML.
Add a child element to the body element and a footer element:
<?php//from w w w .j av a2s .c om
$note=<<<XML
<book>
<name>PHP</name>
<name>Java</name>
</book>
XML;
$xml=new SimpleXMLElement($note);
$xml->body->addChild("date","2013-01-01");
$footer=$xml->addChild("Chapter","Some Chapter");
echo $xml->asXML();
?>
The code above generates the following result.