Replacing adding or adding nodes in SimpleXML
You just can’t! Because you only communicate with SimpleXML by getter and setter methods. It’s possible there is another interface – anywsy i doubt it!
So, the magic of adding nodes is to convert from and to DOMDocument. The snippet below is just something stripped from sourcecode, so you get the idea how it works.
$this->xml = simple_xml_load(”);
// convert base document from simplexml to dom
$dom = dom_import_simplexml($xml)->ownerDocument;
// get our nodes
$newNode = dom_import_simplexml($this->character->xml);
$oldNode = $dom->getElementsByTagName(‘character’)->item(0);
// import replacement node into our working DOM and reassign it
$newNode = $dom->importNode($newNode,true);
// for example: replace the -element in our DOM
$oldNode->parentNode->replaceChild($newNode, $oldNode);
// convert it back
$this->xml = simplexml_import_dom($dom);
Damn ugly but that’s just the way it is! ![]()