PHP addChild () function
PHP SimpleXML Reference Manual
Examples
Body and footer elements to add an element child elements:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</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();
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</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();
?>
Running instance »
Definition and Usage
addChild () function to add a child element to SimpleXML element.
grammar
addChild( name,value,ns );
参数 | 描述 |
---|---|
name | 必需。规定要添加的子元素的名称。 |
value | 可选。规定子元素的值。 |
ns | 可选。规定子元素的命名空间。 |
technical details
return value: | It returns a Add to SimpleXMLElement object in XML child elements. |
---|---|
PHP version: | 5.1.3+ |
PHP SimpleXML Reference Manual