Save XML DOM object to XML file
JCGrafted
Status: New User - Welcome
Joined: 09 Mar 2010
Posts: 2
Location: Houston, TX
Reply Quote
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:

:: Code ::

<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:

:: Code ::

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():

:: Code ::

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:

:: 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!

Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 3753
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Just a quick scan, but if you're seeing 'Dom Object' as the content of the file, it's kind of obvious to me that you're assuming that you can pass a javascript method to php when in fact you're simply assigning the data type to php, in other words, you skipped a step, you have to translate the dom object's CONTENTS explicitly then write them to php, at least that's what it looks like to me.

Whenever I see js give such an output I know I'm assuming that the data in an array or object was going to be passed intact when in fact it doesn't get passed, and all that is known is the type of data holder it is, which is what appears to be the case here.

So add in the explicit handling of the contents prior to inserting it into php as a value is my guess, though I'm too lazy to work the actual logic out myself, that's the part that's work.
Back to top
JCGrafted
Status: New User - Welcome
Joined: 09 Mar 2010
Posts: 2
Location: Houston, TX
Reply Quote
You have restated the problem very succinctly. Thank you.

This is where I am stuck. I do not know how to translate the entire DOM XML object for passing.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 3753
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
I would think you can simply place it into a variable, no? I always avoided ajaxy things, so I'm not really up on some of the methods.

It may be that your core idea is simply wrong, I believe it may by, that is, my faint understanding of ajax suggests that javascript simply passes data to php, which then does the actual file updating.

In other words, you wouldn't be passing the entire xml file contents to php, you'd be updating the file with data that javascript sends directly to the php. That's as close as I remember anyway. In other words, javascript receives the data but php generates the physical file, and php receives the updates and then regenerates the xml file. Javascript isn't supposed to touch files by design, so I suspect that's how the data flow goes. Sounds like you're trying to skip a step here, and have javascript generate the xml file itself, not the content, but again, I'm really not up on Ajax.

Speculating further, I assume what php sends ajax is an object of the xml file, which then ajax extracts data out of. But that object is NOT the xml file itself, it is the data from the file. So it's just meant to work that way, but I'm so rusty on ajax stuff I'll leave it at that in case I'm completely off here.

In theory you could I guess place the data into a post thing but I don't think that's how ajax is supposed to handle xml updates, not positive, but I don't think it is.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours