Current Location: Home> Function Categories> addChild

addChild

Add child elements to SimpleXML elements.
Name:addChild
Category:Uncategorized
Programming Language:php
One-line Description:Add child elements to SimpleXML elements.

Definition and usage

addChild() function is used to add child elements to SimpleXML elements.

Example

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

grammar

 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.
Similar Functions
Popular Articles