Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save XML DOM object to XML file

Status
Not open for further replies.

JCGrafted

Technical User
Nov 10, 2009
4
Hi, thanks for reading.

I am in way over my head! I am trying to write a shopping cart from scratch.

I am trying to use an xml file to save the cart while the user is shopping. I have called a php page in the head of my html file as follows:

<script type="text/javascript" src="createXML.php"></script>

The php page creates a session id, creates an xml file of sessionID.xml, and sends the session id back to the page so that my javascript can know the name of the file. This part works. Here is the code for createXML.php:

session_start(); // start up your PHP session!
$theSessionID = session_id();
touch ($theSessionID.".xml");
echo "var thisSessionID=\".$theSessionID.\";";

I then load the file into a dom object on page load, calling my init function and passing it the var thisSessionID. Here is the init():

function init(IDVariable)
{
setURL(IDVariable); // sets global variable urlName == sessionID.xml

if (window.XMLHttpRequest)
{ xhttp=new XMLHttpRequest(); }
else // for older IE 5/6
{ xhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xhttp.open("GET",urlName,false); // open sessionID.xml
xhttp.send("");
xmlDoc=xhttp.responseXML; // sets global variable xmlDoc as xml object == sessionID.xml
}

I can then manipulate the xml, add and edit nodes, and even display the results. But…I CAN NOT GET THE BLOOMING THING TO SAVE! This is where I am stuck.

I have tried passing the DOM object to another php page using post and the following php code:

$theURL = $_POST["theURL_FromForm"];
$theXML = $_POST["theXMLobject_FromForm"];

$fp = fopen($theURL,"w");
fputs($fp,$theXML);
fclose($fp);

It does overwrite the file, but when I open it to see the contents, instead of my xml content, the file is empty except for the words “DOM Object”.

Please help!

Jonathan
 
[1] The symptom essentially means your form element named "theXMLobject_FromForm" is given a "value" of the Xml DOMDocument object reference which will eventually be posted with the object type name as its serialized value. This is incorrect.

[2] You should properly serialize the DOMDocument and assign its value to the form element named "theXMLobject_FromForm". In the process of assigning the value to it, you should first escape the 5 built-in entities (ie, replace > by "[red]&_g_t_;[/red]" ([red]no underscore[/red]; the site cannot render the escaped form at this moment) etc... before assigning. Then when the form is posted, correct data would be uploaded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top