Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : SimpleXML functions : SimpleXMLElement->addChild()

SimpleXMLElement->addChild()

Adds a child element to the XML node ()

Example 2248. 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();

?>

Related Examples ( Source code ) » simplexml element addchild






Code Examples / Notes » simplexml element addchild

l dot j dot peters

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.


ron @ rotflol dot cx

Replacing a node is an easy mod of the simplexml_append
function simplexml_replace(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->parentNode->replaceChild($node2,$node1);
}


r dot versluis

<?php
// phpversion <= 5.1.2
function simplexml_addChild($parent, $name, $value=''){
$new_child = new SimpleXMLElement("<$name>$value</$name>");
$node1 = dom_import_simplexml($parent);
$dom_sxe = dom_import_simplexml($new_child);
$node2 = $node1->ownerDocument->importNode($dom_sxe, true);
$node1->appendChild($node2);
return simplexml_import_dom($node2);
}
function simplexml_addAttribute($parent, $name, $value=''){
$node1 = dom_import_simplexml($parent);
$node1->setAttribute($name,$value);
return simplexml_import_dom($node1);
}
?>


Change Language


Follow Navioo On Twitter
SimpleXMLElement->addAttribute()
SimpleXMLElement->addChild()
SimpleXMLElement->asXML()
SimpleXMLElement->attributes()
SimpleXMLElement->children()
SimpleXMLElement->__construct()
SimpleXMLElement->getDocNamespaces()
SimpleXMLElement->getName()
SimpleXMLElement->getNamespaces()
SimpleXMLElement->registerXPathNamespace()
SimpleXMLElement->xpath()
simplexml_import_dom
simplexml_load_file
simplexml_load_string
eXTReMe Tracker