954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP, XML, CDATA & HTML entities

I have an XML file like such:

<spice>
	<sections>
		<contact></contact>
		<products></products>
		<people></people>
		<homes></homes>
		<harvest></harvest>
		<contactCopy> 
			<banners><![CDATA[Lots of <u>text</u> with tons of <a href="#">links</a> and <i>other</i> markup.]]></banners>
		</contactCopy>
		<homesCopy>
			<banners><![CDATA[Lots of <u>text</u> with tons of <a href="#">links</a> and <i>other</i> markup.]]></banners>
		</homesCopy>
		<productsCopy>
			<banners><![CDATA[Lots of <u>text</u> with tons of <a href="#">links</a> and <i>other</i> markup.]]></banners>
		</productsCopy>
		<peopleCopy>
			 <banners><![CDATA[Lots of <u>text</u> with tons of <a href="#">links</a> and <i>other</i> markup.]]></banners>
		</peopleCopy>
		<harvestCopy>
			<banners><![CDATA[Lots of <u>text</u> with tons of <a href="#">links</a> and <i>other</i> markup.]]></banners>
		</harvestCopy>	
	</sections>
</spice>


What I need to be able to do is pull the CDATA content out, WITH the tags intact, have the client edit the content with a markup editor and put unentitied HTML back into the CDATA section. I have been able to pull the content out of the CDATA field with

$doc = new DomDocument();
$file = "spice.xml";
$doc->load($file);
$xPath = new DomXPath($doc);
$homesCopy 	= $xPath->query("//sections/homesCopy/banners");
echo $homesCopy->item(0)->nodeValue;


but I cannot for the life of me figure out how to simply edit that content and put it back without turning the HTML tags into their entities.

tomccabe
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
. I have been able to pull the content out of the CDATA field with


How do you know which banner you are editing at any given time? I was expecting to see each banner node with a unique id attribute so that when you edit, you would know which node to update. Where's your attempted update code?

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 
assuming you are POSTing from a form with <input name='homesCopy'> try:

<?php
$doc = new DomDocument();
$file = "spice.xml";
$doc->load($file);
$xPath = new DomXPath($doc);
$homesCopy 	= $xPath->query("//sections/homesCopy/banners")->item(0);
if( isset($_POST['homesCopy']) ){
  $homesCopy->firstChild->nodeValue=$_POST['homesCopy'];
  $doc->save($file);
}
echo $homesCopy->nodeValue;
?>
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: