PHP addAttribute()関数
例
ルート要素と body 要素に属性を追加する:
<?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);
$xml->addAttribute("type","private");
$xml->body->addAttribute("date","2013-01-01");
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);
$xml->addAttribute("type","private");
$xml->body->addAttribute("date","2013-01-01");
echo $xml->asXML();
?>
例を実行 »
定義と用法
addAttribute() 関数は SimpleXML 要素に属性を追加します。
構文
addAttribute(name,value,ns);
| パラメータ | 説明 |
|---|---|
| name | 必須。追加する属性の名前を指定します。 |
| value | オプション。属性の値を指定します。 |
| ns | オプション。属性の名前空間を指定します。 |
技術詳細
| 戻り値: | 戻り値はありません。 |
|---|---|
| PHP バージョン: | 5.1.3+ |
PHP SimpleXML リファレンスマニュアル その他の拡張