tags:

views:

52

answers:

2

Hi,

I have an XML file with some CDATA nodes. I want to change the text inside the CDATA node (keeping it as CDATA node). So, I guess I first need to read the CDATA node and then write it back. But, I am not sure how to do that in PHP. I was able to create a new CDATA node but how can I edit a CDATA node? Is there a direct way to do that?

Thanks.

+2  A: 

I'm not versed in PHP (lots of Java DOM experience) but I think you need to replace the text node with a new CDATA text node. See

http://www.php.net/manual/en/domdocument.createcdatasection.php

and

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

Jim Garrison
A: 

I fixed it on my own:

$nodes = $xml->getElementsByTagName('tagname');

$oldTitleNode = null; 
$newTitleNode = null;

//Iterate for each <title> tag 
foreach ($nodes as $node) {     
   if ($node->parentNode->getAttribute('name')== $tag_name_value){

       $oldTitleNode = $node;

      //Create new CDATA Node
      $newTitleNode=$node->parentNode->appendChild($xml->createElement('tagname'));
      $cdata=$xml->createCDATASection($update_title);
      $newTitleNode->appendChild($cdata);

      //Replace the Existing CDATA Node
     $node->parentNode->replaceChild($newTitleNode, $oldTitleNode);
   } 
}
Blueboye