PHP addAttribute() 函数
定义和用法
addAttribute()
函数用于向 SimpleXML 元素添加属性。
实例
向根元素(<note>)和 <body> 元素添加属性:
<?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);
// 向根元素添加属性
$xml->addAttribute("type","private");
// 向 body 元素添加属性
$xml->body->addAttribute("date","2014-01-01");
echo $xml->asXML();
?>
语法
SimpleXMLElement::addAttribute(name, value, ns)
参数 | 描述 |
---|---|
name | 必需。指定要添加的属性的名称。 |
value | 可选。指定属性的值。 |
ns | 可选。指定属性的命名空间。 |
技术细节
返回值: | 无返回值。 |
---|---|
PHP 版本: | 5.1.3+ |