PHP addChild() 函数
定义和用法
addChild()
函数用于向 SimpleXML 元素添加子元素。
实例
向 <body> 元素添加子元素,并添加一个新的 <footer> 元素:
<?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);
// 向 body 元素添加子元素
$xml->body->addChild("date","2016-01-01");
// 在 note 内部的最后一个元素后添加子元素
$footer = $xml->addChild("footer","Some footer text");
echo $xml->asXML();
?>
语法
SimpleXMLElement::addChild(name, value, ns)
参数 | 描述 |
---|---|
name | 必需。指定要添加的子元素的名称。 |
value | 可选。指定子元素的值。 |
ns | 可选。指定子元素的命名空间。 |
技术细节
返回值: | 返回表示添加到 XML 节点的子元素的 SimpleXMLElement 对象。 |
---|---|
PHP 版本: | 5.1.3+ |