<?php
$xmlLoc = $_GET['g'];

$file = fopen($xmlLoc, "w+") or die("Can't open XML file");

// create array containing <p> tags that get decoded
$oldChars = array('&lt;p&gt;', '&lt;/p&gt;');

// since I use CDATA and <p> tags always together - I create an array
// of what I want to replace the decoded <p> tags with.
$newChars = array('<![CDATA[<p>', '</p>]]>');

// use the PHP string replace function to replace every decoded opening
// <p> tag with a CDATA tag followed by a <p> tag, and also repeat this
// with the closing </p>, replacing it with an encoded </p> tag followed
// by a closing CDATA tag.
$xmlcdRep = str_replace($oldChars, $newChars, $HTTP_RAW_POST_DATA);

// use html entity encode to fix the remaining decoded html tags
$xmlString = html_entity_decode($xmlcdRep);


// hopefully by here - the decoded tags will be properly reformatted and
// the xml can saved back to the server correctly
// the only thing i don't know how to do is to preserve the visual
// spacing structure so if you open the xml in dreamweaver or text
// editor, it still retains it's original well laid out form.
if(!fwrite($file, $xmlString)){
print "<?xml version=\"1.0\" encoding=\"utf-8\"?><output>Error writing to XML-file</output>";
}else{
print "<?xml version=\"1.0\" encoding=\"utf-8\"?><output>XML File Saved</output>";
}
fclose($file);
?>