PHP addChild() 関数

❮ PHP SimpleXML リファレンス

<body>要素と新しい<footer>要素に子要素を追加します:

<?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","2014-01-01");

// Add a child element after the last element inside note
$footer = $xml->addChild("footer","Some footer text");

echo $xml->asXML();
?>
例の実行 »

定義と用法

addChild() 関数は、SimpleXML要素に子要素を追加します。


構文

addChild(name,value,ns);

パラメータ 説明
name 必須。追加する子要素の名前を指定する
value 任意。追加する子要素の値を指定する
ns 任意。子要素の名前空間を指定する

技術内容
返り値: XMLに追加された子を表すSimpleXMLElementオブジェクトを返します
PHP バージョン: 5.1.3+

❮ PHP SimpleXML リファレンス