We help IT Professionals succeed at work.

Writing a properly formatted XML in Javascript

rgarimella
rgarimella used Ask the Experts™
on
Hi Folks,

I need to build an XML file from Javascript Variables. Basically text stored in variables.

How do i do that?

Any examples is appreciated.

Thanks
RG
<question>
javascriptvar
</question>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
You may start here : http://www.w3schools.com/xml/xml_dom_advanced.asp

(...but you will have two main links : previous and next)

Commented:
Hi there! as far as I know, its not possible to create a Physical XML File with javascript,  i.e. myfile.xml
What do you want the file for? do you want to store it @ the server? or let the user download it right away? either way, i recommend using json to store the variables and then serialize it and post it to the server, in the server you can create the file, store it in some server folder and then give a link to the user so it can be downloaded.

Are you using any javascript library? give us more info :D

Author

Commented:
Hi sosamv,

Yes I know that I cannot save from javascript to a physical XML file.

Do you have some examples on how to use JSON, to store javascript values/variables and send it to a database. I do not need the code for saving into a database. Just need examples on how to use JSON if you have any.

Thx

Commented:
Sure, JSON helps us to create complex data structures like XML in javascript.

JSON Tutorial: http://www.learn-ajax-tutorial.com/Json.cfm
Serializing JSON: http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/

It's very important for you to learn JSON properly, cuz thats what ajax is all about.
//You can start creating an Object (namespace)
var xml = {};
//then, you can dynamically add items to if i.e.

for(var i = 0; i < 10; i++){
	xml.items[i] = i
}
//Use Firefox and install firebug to see this in the console
console.log(xml);
//Now you have a "xml" variable, that has a items array in it.
//In order for you to add new properties (or DOM tags Like XML) do the following

xml["myNewProperty"] = "TEST";
alert(xml.myNewProperty); //alerts test


/*when you have your JSON object ready, you need to serialize de Object using something like this: http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization . With this, you can convert the object to string
and the result will be in this format ---> "{items:[0,1,2,3,4,5,6,7,8,9], myNewProperty:"TEST"}" and this is what you port to the server.
In the server its easy to use this string in PHP for example, you use json_encode(jsonvar_recieved) and it will create an array with all of the json information
*/

Open in new window

Commented:
My previous example was wrong haha sorry:
//You can start creating an Object (namespace)
var xml = {
   items:[]
};
//then, you can dynamically add items to if i.e.

for(var i = 0; i < 10; i++){
	xml.items[i] = i
}
//Use Firefox and install firebug to see this in the console
console.log(xml);
//Now you have a "xml" variable, that has a items array in it.
//In order for you to add new properties (or DOM tags Like XML) do the following

xml["myNewProperty"] = "TEST";
alert(xml.myNewProperty); //alerts test


/*when you have your JSON object ready, you need to serialize the Object using something like this: http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization . With this, you can convert the object to string
and the result will be in this format ---> "{items:[0,1,2,3,4,5,6,7,8,9], myNewProperty:"TEST"}" and this is what you post to the server.
In the server its easy to use this string in PHP for example, you use json_encode(jsonvar_recieved) and it will create an array with all of the json information
*/

Open in new window