addChild
Add child elements to SimpleXML elements.
addChild()
function is used to add child elements to SimpleXML elements.
Add a child element to the <body> element and add a new <footer> element:
<?php $note = <<< XML <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Do not forget the meeting!</body> </note> XML ; $xml = new SimpleXMLElement ( $note ) ; // Add child elements to body element $xml -> body -> addChild ( "date" , "2016-01-01" ) ; // Add child elements after the last element inside the note $footer = $xml -> addChild ( "footer" , "Some footer text" ) ; echo $xml -> asXML ( ) ; ?>
Run the instance
SimpleXMLElement :: addChild ( name , value , ns )
parameter | describe |
---|---|
name | Required. Specifies the name of the child element to be added. |
value | Optional. Specifies the value of the child element. |
ns | Optional. Specifies the namespace for the child element. |