Current Location: Home> Function Categories> addAttribute

addAttribute

Add attributes to SimpleXML elements.
Name:addAttribute
Category:Uncategorized
Programming Language:php
One-line Description:Add attributes to SimpleXML elements.

Definition and usage

addAttribute() function is used to add attributes to SimpleXML elements.

Example

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

grammar

 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.
Similar Functions
Popular Articles