Jump to content

[DOM] ReplaceChild


Doccie

Recommended Posts

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.