PHP DomDocument replaceChild

I can't get this method to work.

I have this PHP code:

        $dom = new \DOMDocument();
        $dom->loadHTML($this->_html);

        $newD = $viewElement->replaceHtml();


        $oe = $dom->getElementById("{{ALViewElement_resume}}");
        $ne = $newD->getElementById("resume");


        echo $dom->saveHTML();
        echo $dom->saveHTML($oe);
        echo $newD->saveHTML($ne);
        echo $dom->saveHtml($oe->parentNode);
        $oe->parentNode->replaceChild($ne, $oe);

        echo $dom->saveHTML($ne);
        echo $dom->saveHTML();

So I create a DomDocument, and load some html. I then call $viewElement->replaceHtml() which is a function I have elsewhere that returns another DomDocument Object.

Then $oe is the element I have originally in the passed in html and $ne is the element in the DomDocument that was created in my replaceHtml call. I want to replace $oe with $ne.

I then have all these echos. The first one will show what $dom to begin with, and that's correct.

The next will show you the original element (the one I want to replace) and that's correct. Notice the id starts with {{ALViewElement_.

The next shows you the element I want to replace the original element with. Notice it's like the same thing except the id doesn't start with ALViewElement. And no I am not looking for answers saying I can change the id with DomDocument very easily because that's not what I'm looking for here.

The next call shows the parentNode of the original element. It was just an html fragment so there actually is no parentNode. This prints itself. Notice the {{ALViewElement_ id.

I then call replace child.

Then the next two echos print nothing. replaceChild didn't work and I lost everything.

Why is this not working? What am I missing?

EDIT: Forgot the html.

I put them in a jsFiddle so it's easy to see: http://jsfiddle.net/jDPTC/

you should clone your new node first, e.g.

$oe->parentNode->replaceChild($ne->cloneNode(true), $oe);

http://www.php.net/manual/en/domnode.clonenode.php