bloginfo('name');

bloginfo('description');

Replacing adding or adding nodes in SimpleXML

November 18th, 2008 by Blu:RayNe

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 document is emtpy –  it just illustrates an example!
$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! :(

Filed under Allgemein, PHP having No Comments »

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.