Doccie Posted May 22, 2007 Share Posted May 22, 2007 I've literally been trying to get this CDATA node replaced for hours, but PHP keeps throwing different exceptions at me (ranging from DOM_NOT_FOUND to Call of function replaceChild on a Non-Object). If someone would be able to help, I would be very grateful. I have a fairly simple XML file <root> <parent> <child> <html> <![CDATA[some html]]> </html> </child> ... </parent> ... </root> and I want to replace a certain CDATA element with another one. And I am trying to do so, by use of this code: $dom_xml = dom_import_simplexml($xml); $dom = new DOMDocument('1.0'); $dom_xml = $dom->importNode($dom_xml, true); $dom_xml = $dom->appendChild($dom_xml); $newNode = $dom->createElement('html'); $data = $dom->createCDATASection($op['new'][$k]); $new = $newNode->appendChild($data); $lvl1 = $dom->getElementsByTagName('parent'); $l1 = $lvl1->item($op['l2'][$k]); //this ofcourse, being an integer $lvl2 = $l1->getElementsByTagName('child'); $l2 = $lvl2->item($op['l3'][$k]); //this ofcourse, being an integer $html = $l2->getElementsByTagName('html'); $html = $html->item(0); $l2->parentNode->replaceChild($new, $l2); $xml = simplexml_import_dom($dom); Yes, I am aware that I am most likely in violation of several best practices, but for now, I'm just trying to get this thing to work Quote Link to comment https://forums.phpfreaks.com/topic/52503-dom-replacechild/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.