addAttribute
向SimpleXML 元素添加屬性。
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 | 可選。指定屬性的命名空間。 |