downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

SimpleXMLElement::asXML> <SimpleXMLElement::addAttribute
Last updated: Fri, 20 May 2011

view this page in

SimpleXMLElement::addChild

(PHP 5 >= 5.1.3)

SimpleXMLElement::addChild Añade un elemento hijo al nodo XML

Descripción

public SimpleXMLElement SimpleXMLElement::addChild ( string $name [, string $value [, string $namespace ]] )

Añade un elemento hijo al nodo y retorna un SimpleXMLElement del hijo.

Parámetros

name

Nombre del elemento hijo a añadir.

value

Si se especifica, valor del elemento hijo.

namespace

Si se especifica, el namespace al que pertenece el elemento hijo.

Valores devueltos

El método addChild retorna un objeto SimpleXMLElement representando el hijo añadido al nodo XML.

Ejemplos

Note:

Los ejemplos listados quizá incluyen example.php, que hacen referencia a la cadena XML encontrada en el primer ejemplo de la guía de uso básico.

Example #1 Añade atributos e hijos a un elemento SimpleXML

<?php
include 'example.php';
 
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('tipo''documental');

$pelicula $sxe->addChild('pelicula');
$pelicula->addChild('titulo''PHP2: Más historias sobre Parser');
$pelicula->addChild('argumento''Todo sobre las personas que hacen que funcione.');

$personajes $pelicula->addChild('personajes');
$personaje  $personajes->addChild('personaje');
$personaje->addChild('nombre''Sr. Parser');
$personaje->addChild('actor''John Doe');

$puntuacion $pelicula->addChild('puntuacion''5');
$puntuacion->addAttribute('tipo''estrellas');
 
echo 
$sxe->asXML();

?>

El resultado del ejemplo sería algo similar a:

<?xml version="1.0" standalone="yes"?>
<peliculas tipo="documental">
 <pelicula>
  <titulo>PHP: Tras el Parser</titulo>
  <personajes>
   <personaje>
    <nombre>Srta. Programadora</nombre>
    <actor>Onlivia Actora</actor>
   </personaje>
   <personaje>
    <nombre>Sr. Programador</nombre>
    <actor>El Actor</actor>
   </personaje>
  </personajes>
  <argumento>
   Así que, este lenguaje. Es como, un lenguaje de programación. ¿O es un
   lenguaje interpretado? Lo descubrirás en esta intrigante y temible parodia
   de un documental.
  </argumento>
  <grandes-lineas>
   <linea>PHP soluciona todos los problemas web</linea>
  </grandes-lineas>
  <puntuacion tipo="pulgares">7</puntuacion>
  <puntuacion tipo="estrellas">5</puntuacion>
 </pelicula>
 <pelicula>
  <titulo>PHP2: Más historias del Parser</titulo>
  <argumento>Todo sobre la gente que lo hace funcionar.</argumento>
  <personajes>
   <personaje>
    <nombre>Sr. Parser</nombre>
    <actor>John Doe</actor>
   </personaje>
  </personajes>
  <puntuacion type="estrellas">5</puntuacion>
 </pelicula>
</peliculas>

Ver también



SimpleXMLElement::asXML> <SimpleXMLElement::addAttribute
Last updated: Fri, 20 May 2011
 
add a note add a note User Contributed Notes SimpleXMLElement::addChild
Volker Grabsch 21-Apr-2011 01:17
Note that although addChild() escapes "<" and ">", it does not escape the ampersand "&".

So addChild() is unsuited to handle user-defined input!

Instead, you will have to replace all "&" with "&amp;" before calling addChild().

Or, use htmlspecialchars() which also replaces other characters, but won't do any harm as addChild() won't replace those again.
felipenmoura at gmail dot com 12-Apr-2011 01:06
This method returns a reference to the specific SimpleXMLElement.
If you use:
<?php
    $xml
= new SimpleXMLElement('<root></root>');
   
$xml->father['name']= 'Fathers name'; // creates automatically a father tag with attribute name
   
$son= $xml->father->addChild('son'); // uses the first father tag
   
$son['name']= 'first son';
   
$otherSon= $xml->father->addChild('son'); // uses the first father tag but now, in a second son tag
   
$otherSon['name']= 'second son';
   
    echo
htmlentities($xml->asXML());
?>

The result will be
<root>
    <father>
        <son name='first son' />
        <son name='second son' />
    </father>
</root>

So, once you change something to the just added child, you are actually accessing the element inside the SimpleXMLElement as a reference.
Dmitry Dulepov 02-Sep-2009 12:53
addChild will automatically escape all XML characters as necessary. You do not have to do any magic when adding values like "if a > b then...".

 
show source | credits | stats | sitemap | contact | advertising | mirror sites