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+ |
