addAttribute
Add attributes to SimpleXML elements.
addAttribute()
function is used to add attributes to SimpleXML elements.
Add attributes to the root element (<note>) and <body> elements:
<?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 ) ; // Add attributes to the root element $xml -> addAttribute ( "type" , "private" ) ; // Add attributes to body element $xml -> body -> addAttribute ( "date" , "2014-01-01" ) ; echo $xml -> asXML ( ) ; ?>
Run the instance
SimpleXMLElement :: addAttribute ( name , value , ns )
parameter | describe |
---|---|
name | Required. Specifies the name of the attribute to add. |
value | Optional. Specifies the value of the attribute. |
ns | Optional. Specifies the namespace for the property. |