![]() |
Saving a Multidimensional Array into an XML using JavaScript
So I have a multidimensional array that looks like this:
Code:
var map = [[0, 0, 0, 0, 0, 0, 0],My XML file looks like the following: Code:
<TileMaps>Code:
<Level><map> ARRAY HERE </map></Level>Thanks |
JavaScript has no ability to WRITE to files.
Not 100% true; MSIE can write to files ON THE USER'S COMPUTER (only), but only if the user marks your site as being a "safe" site and only if he/she agrees to let your page run an "unsafe for scripting" ActiveX control. But even here, the JS code is only writing to the USER's computer. No matter what, JavaScript has no ability to write to files ON THE SERVER, which is surely what you would want to do for your map game. You will need server-side code (PHP/ASP/JSP) to do this. And then the JavaScript code *can* send the data to the server and let the server do the file writing. |
Go with whatever server side you're using, if at all possible. It will have no problem writing to the file. You have to open the file for writing, you can make a template file for the heading, copy it to the folder you want it in, with rename, write what you want to it, and append the closing tags. Done.
|
Actually, if the server side system you are using understands XML well, you can load the XML file into an XML document, *append* the new <Level> with its subnodes, and then write the XML document back out as XML text. Trivial to do in ASP.NET or JSP. Not very hard to do in classic ASP. You then don't have to parse the XML in the file by hand to try and find the closing tag.
(I don't use PHP, so I don't know where this is easy or hard there. If it's not easy--assuming you have the right libraries--I would be surprised.) |
Quote:
- The OP is another victim of the ambiguous JavaScript != JScript - The OP has control over the server and is able to flag server directories as writable - The deployment server supports classic ASP (and thus JScript (which is *remarkably* similar to JavaScript)) - Only 1 user at a time can access the "savemapdata" page (it is not a data base, although i leave that as and exercise for the better qualified) inc/json.asp Code:
<%Code:
<%Code:
<%@LANGUAGE="JavaScript"%><!--Dont let the name fool ya...-->Code:
<%@LANGUAGE="JavaScript"%>At a second glance I suppose the savemapdata.asp and maps.asp could be combined to produce an actual .xml as requested... savemapdata.asp Code:
<%@LANGUAGE="JavaScript"%>anyway, my batteries are dying, I hope I didn't miss anything. I tested all those pages individually (except the last) so they should work in concert. |
Here's my solution, it uses PHP in the backend, and JSON, so you'll want to modify your server side code as appropriate (if you wrap the JSON in XML, you'll also need to adjust the syntax in the AJAX pieces)...oh, depending on your target user, you'll want to add some validation to the inputs (both client and server side):
PHP Code:
Best Regards, Navin Patel |
| All times are GMT +1. The time now is 04:21 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.