2

i want to just change ABCD written inside cdata of data.xml file with a new value held by $change by php. i am able to get all cdata value using the following code but don't know how to change and save it.

<?php $doc = new DOMDocument(); $doc->load('data.xml'); $destinations = $doc->getElementsByTagName("text"); foreach ($destinations as $destination) { foreach($destination->childNodes as $child) { if ($child->nodeType == XML_CDATA_SECTION_NODE) { echo $child->textContent . "<br/>"; } } } ?> 

my data.xml file.

<?xml version="1.0" encoding="utf-8"?> <data displayWidth="" displayHeight="" frameRate="" fontLibs="assets/fonts/Sansation_Regular.swf"> <preloader type="snowflake" size="40" color="0xffffff" shapeType="circle" shapeSize="16" numShapes="8"/> <flipCard openingDuration="2" openCardZ="400" tweenMethod="easeOut" tweenType="Back"> <video videoPath="video.MP4" frontImage="assets/christmas/front.jpg" videoPoster="assets/christmas/videoPoster.jpg" videoFrame="assets/christmas/videoFrame.png" bufferTime="10" loopVideo="true"/> <flips backImage="assets/christmas/back.jpg" backColor="0x808080"> <flip isText="true" xPos="600" yPos="470" openDelay="8" openDuration="2" tweenMethod="easeOut" tweenType="Elastic" action="link" url="http://activeden.net/"> <text id="name" font="Sansation_Regular" embedFonts="true" size="40" color="0x802020"><![CDATA[ABCD]]></text> </flip> <flip isText="true" xPos="300" yPos="30" openDelay="2" openDuration="2" tweenMethod="easeOut" tweenType="Elastic"> <text font="Sansation_Regular" embedFonts="true" size="80" color="0x202020"><![CDATA[HAPPY]]></text> </flip> </flips> </flipCard> </data> 

2 Answers 2

3

You change the text inside a CDATA section by setting the nodeValue of that CDATA node (DOMCdataSection in PHP):

$child->nodeValue = $change; 

Output (excerpt & simplified):

 ... <flip isText="true" xPos="600" yPos="470" openDelay="8" openDuration="2" tweenMethod="easeOut" tweenType="Elastic" action="link" url="http://activeden.net/"> <text id="name" ... color="0x802020"><![CDATA[changed ABCD]]></text> </flip> <flip isText="true" xPos="300" yPos="30" openDelay="2" openDuration="2" tweenMethod="easeOut" tweenType="Elastic"> <text font="Sansation_Regular" ... ><![CDATA[changed HAPPY]]></text> </flip> ... 

For your second question you have on how to save the document: The method to save the XML document is DOMDocument::save:

$filename = '/path/to/file.xml'; $doc->save($filename); 
Sign up to request clarification or add additional context in comments.

4 Comments

yes your code it working but adding -new at last but i want to replace ABCD with new value not adding -new at last.
@shubhjaiswal: Naturally that was only exemplary, the more important part is that you set the field named nodeValue of the DOMCdataSection object you have in $child to the string value of your wish and dreams. E.g. $change, see as well the edited answer which now refers to that variable namely, just in case you still have problems to understand how the assignment ("setting") with a different expression (in your case the $change variable) would look like. Let me know if you still have troubles.
now its working fine after your second edit. but its changing both cdata value how to change only one(abcd). code <?php $doc = new DOMDocument(); $xml='data.xml'; $doc->load($xml); $destinations = $doc->getElementsByTagName("text"); foreach ($destinations as $destination) { foreach($destination->childNodes as $child) { if ($child->nodeType == XML_CDATA_SECTION_NODE) { echo $child->textContent . "<br/>"; $child->nodeValue = "new value"; } } } $doc->save($xml); ?>
@shubh jaiswal: Well, naturally it changes as many or little CDATA nodes as you write down in your code to change - and also to the values you command it to change. Change the commands if you're not yet confident the way it works to the right order. I assume you can benefit from learning about XPath to query only the one element you want to change. However what you've asked for - how to change the text of a CData node - is already answered completely and I also already commented back effectively on your first comment. There is a reason why you should keep your questions concrete on SO.
1

Answer from @Hakre is correct. just want to update final working code because following code will only update my ABCD value not all.

<?php $doc = new DOMDocument(); $xml='data.xml'; $doc->load($xml); $destinations = $doc->getElementsByTagName("text"); foreach ($destinations as $destination) { foreach($destination->childNodes as $child) { if ($child->nodeType == XML_CDATA_SECTION_NODE) { echo $child->textContent . "<br/>"; $child->nodeValue = "new value"; } }break; } $doc->save($xml); ?> 

1 Comment

Looks sane. You can also move the break directly after the part where you're done, that is after the assignment of the new value: $child->nodeValue = "new value"; (newline) break;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.