Función addAttribute() de PHP
Definición y uso
La función addAttribute() agrega un atributo a un elemento SimpleXML.
Esta función no devuelve ningún valor.
Sintaxis
class SimpleXMLElement { string addAttribute(name,value,ns) }
Parámetros | Descripción |
---|---|
name | Obligatorio. Especificar el nombre del atributo. |
value | Obligatorio. Especificar el valor del atributo. |
ns | Opcional. Especificar el espacio de nombres del atributo. |
Ejemplo
Archivo XML:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Recordatorio</heading> <body>¡No olvides la reunión!</body> </note>
Código PHP:
<?php $xml = simplexml_load_file("test.xml"); $xml->body[0]->addAttribute("type", "small"); foreach($xml->body[0]->attributes() as $a => $b) { echo $a,'="',$b,'"'; } ?>
Salida:
type="small"