PHP addAttribute()Función
Manual de referencia de PHP SimpleXML
Ejemplo
Agregar un atributo al elemento raíz y al elemento body:
<?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();
?>
$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();
?>
Ejecutar ejemplo »
Definición y uso
La función addAttribute() agrega un atributo a un elemento SimpleXML.
Sintaxis
addAttribute(name,value,ns);
| Parámetros | Descripción |
|---|---|
| name | Obligatorio. Especifica el nombre del atributo a agregar. |
| value | Opcional. Especifica el valor del atributo. |
| ns | Opcional. Especifica el espacio de nombres del atributo. |
Detalles técnicos
| Valor de retorno: | No devuelve ningún valor. |
|---|---|
| Versión de PHP: | 5.1.3+ |
Manual de referencia de PHP SimpleXML Otras extensiones