PHP addChild() 函式
示例
向 <body> 元素新增一個子元素,並新增一個新的 <footer> 元素
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml = new SimpleXMLElement($note);
// 向 body 元素新增一個子元素
$xml->body->addChild("date","2014-01-01");
// 在 note 中的最後一個元素後新增一個子元素
$footer = $xml->addChild("footer","Some footer text");
echo $xml->asXML();
?>
執行示例 »
定義和用法
addChild() 函式向 SimpleXML 元素追加一個子元素。
語法
SimpleXMLElement::addChild(name, value, ns)
引數值
引數 | 描述 |
---|---|
name | 必需。指定要新增的子元素的名稱 |
value | 可選。指定子元素的值 |
ns | 可選。指定子元素的名稱空間 |
技術詳情
返回值 | 一個 SimpleXMLElement 物件,代表新增到 XML 節點中的子元素 |
---|---|
PHP 版本 | 5.1.3+ |
❮ PHP SimpleXML 參考