PHP: SimpleXMLElement->addChild() - Manual

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

search for in the

SimpleXMLElement->asXML() SimpleXMLElement->addAttribute()

Last updated: Sat, 27 Jan 2007

view this page in

# SimpleXMLElement->addChild()

(no version information, might be only in CVS)

SimpleXMLElement->addChild() -- Adds a child element to the XML node

## Описание

class SimpleXMLElement {
SimpleXMLElement addChild ( string name [, string value [, string namespace]] )
}

Adds a child element to the node and returns a SimpleXMLElement of the child.

## Список параметров

`name`

  The name of the child element to add.

`value`

  If specified, the value of the child element.

`namespace`

  If specified, the namespace to which the child element belongs.

## Возвращаемые значения

The `addChild` method returns a SimpleXMLElement object representing the child added to the XML node.

## Примеры

Пример 1. Add attributes and children to a SimpleXML element

`<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo $sxe->asXML();
?>`

## Смотрите также

SimpleXMLElement->addAttribute()

add a note User Contributed Notes
SimpleXMLElement->addChild()
l dot j dot peters at student dot utwente dot nl
31-Jan-2007 08:51
`Rob Richards and I have come up with a very easy way to append one SimpleXML tree to another:
<?php
function simplexml_append(SimpleXMLElement $parent, SimpleXMLElement $new_child){
$node1 = dom_import_simplexml($parent);
$dom_sxe = dom_import_simplexml($new_child);
$node2 = $node1->ownerDocument->importNode($dom_sxe, true);
$node1->appendChild($node2);
}
?>
And adding a textnode is also easy:
<?php
//$sxe1 (refers to SimpleXMLElement)
$node1 = dom_import_simplexml($sxe1);
$node1->appendChild(new DOMText("my text"));
?>
Ofcourse, you could use this to extend the SimpleXMLElement class and overwrite and improve the existing addChild function.
Have fun, Luuk Peters.`
thomas dot ekdahl at empatix dot no
18-Sep-2006 05:57
`Because SimpleXML transforms the values &amp; to & whe reading from file or string, the values becomes invalid for use when adding childs later on. This simple conversion to the example above fixes this problem.
class XMLElement extends SimpleXMLElement
{
public function addElement (SimpleXMLElement $xmlTree,$root=false)
{
if($root)
{
$child = $this->addChild ($xmlTree->getName());
foreach ($xmlTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($xmlTree);
}
else
{
foreach ($xmlTree as $childName => $childTree)
{
$child = $this->addChild($childName,$this->fix_content((string) $childTree)); // this is not comletely correct
foreach ($childTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($childTree->children());
}
}
}
#When the xml is read by simplexml_load_file or simplexml_load_file the values that contains &amp; is converted to &. And then it is not possible to add these values without getting errors.
#THis function should fix this
public function fix_content($value) {
return str_replace('&', '&amp;', $value);
}
}`
fred at misterbob dot nl
03-Aug-2006 09:23
`<?php
class XMLElement extends SimpleXMLElement {
public function addElement (XMLElement $xmlTree,$root=false) {
if ($root) {
$child = $this->addChild ($xmlTree->getName());
foreach ($xmlTree->attributes() as $k => $v) {
$child->addAttribute($k,$v);
}
$child->addElement($xmlTree);
} else {
foreach ($xmlTree as $childName => $childTree) {
$child = $this->addChild($childName,(string) $childTree); // this is not comletely correct
foreach ($childTree->attributes() as $k => $v) {
$child->addAttribute($k,$v);
}
$child->addElement($childTree->children());
}
}
}
}
$xmlMovies = "<movies><movie name=\"Shawshank Redemption\"><plot>The life of Andy Dufresne changes when he is convicted and jailed for the murder of his wife.</plot></movie></movies>";
$elMovies = new XMLElement($xmlMovies);
$xmlGodfather = "<movie name=\"Godfather\"><plot>The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.</plot></movie>";
$elGodfather = new XMLElement ($xmlGodfather);
$elMovies->addElement($elGodfather,true);
echo $elMovies->asXML();
?>
Method to append (Simple)XMLElements to each other.`
add a note
SimpleXMLElement->asXML() SimpleXMLElement->addAttribute()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST