Add an attribute to the root element and body element:
<?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( );?>The addAttribute() function adds an attribute to a SimpleXML element.
addAttribute( name,value,ns );
parameter | describe |
---|---|
name | Required. Specifies the name of the attribute to be added. |
value | Optional. Specifies the value of the attribute. |
ns | Optional. Specifies the namespace of the attribute. |
Return value: | There is no return value. |
---|---|
PHP version: | 5.1.3+ |