PHP simplexml: Edit an XML file, big problem with CDATA
Posted: Tue Dec 01, 2009 8:11 am
I'm trying to create a very simple web page that will load an xml file, displace a nodes content in a html paragraph text box form - allowing a user to edit the text - and after clicking 'submit' have php rewrite the nodes content.
I've manage to do just that, however I need the text to be wrapped in CDATA tags as it'll later be used in a Flash website. Also the original xml file has CDATA tags but when php writes back to the node it loses the CDATA bit.
I'm really confused about it all, I'm just php beginner, I've spent 8 hours googling it trying to learn about what's going on, but I'm still clueless - even more so now...
So here's what I've got:
An XML file:
A html/php page with:
I've tried:
but it comes out looking like this in the XML file:
Someone please help, please explain it all to me....
I've manage to do just that, however I need the text to be wrapped in CDATA tags as it'll later be used in a Flash website. Also the original xml file has CDATA tags but when php writes back to the node it loses the CDATA bit.
I'm really confused about it all, I'm just php beginner, I've spent 8 hours googling it trying to learn about what's going on, but I'm still clueless - even more so now...
So here's what I've got:
An XML file:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<site>
<aboutpage>
<copya><![CDATA[This is the about page text.]]></copya>
</aboutpage>
</site>
Code: Select all
<?php
$file = 'sitecontent.xml';
$xml = simplexml_load_file($file);
if (!isset($_POST['submit'])) {
}else{
$xml->aboutpage[0]->copya = $_POST["element_1"];
$xml->asXML("sitecontent.xml");
}
?>
<form id="form_1" class="abc" method="post" action="<?php echo $PHP_SELF;?>">
<textarea id="element_1" name="element_1" class="element textarea medium"><?php echo $xml->aboutpage[0]->copya;?></textarea>
<input id="saveForm" class="button_text" type="submit" name="submit" value="Save" />
</form>
Code: Select all
$xml->aboutpage[0]->copya = "<![CDATA[" . $_POST["element_1"] . "]]>";and I'm having problems with " turning into \"<![CDATA[This is the about page text now edited.]]>
Someone please help, please explain it all to me....