How do i create new xml file and also modify any xml file means add more nodes in xml file using javascript.?

Thanks in advance...

share|improve this question

4 Answers

I've found Ariel Flesler's XMLWriter constructor function to be a good start for creating XML from scratch, take a look at this

http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html

Example

function test(){    
   var v = new  XMLWriter();
   v.writeStartDocument(true);
   v.writeElementString('test','Hello World');
   v.writeAttributeString('foo','bar');
   v.writeEndDocument();
   console.log( v.flush() );
}

Result

<?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>
<test foo="bar">Hello World</test>

One caveat to keep in mind is that it doesn't escape strings.

See also Libraries to write xml with JavaScript

share|improve this answer
how to save this xml file or where it will be saved in default – praveen singh Aug 10 '12 at 6:44
An updated version of the library is maintained at: github.com/alexandern/XMLWriter – koppor Feb 19 at 22:54

In IE you can manipulate XML using an ActiveX.
There is also a built in object for FF and other W3C complient browsers.
I recommend you to take a look at this article.

share|improve this answer
Nice article thanks it teach me a lot.. – Avinash Jul 28 '09 at 9:15

What context is the file running in, and where are you going to save the new XML data?

(The usual context is the browser, in which case you're basically able to display it or post it back to the server.)

But if you're writing a script that would run outside the browser, it depends.

share|improve this answer
i am getting data of the google map, and need to store in xml file, and from the xml file i need to create map based on xml element.. – Avinash Jul 28 '09 at 7:00
1  
Okay, I guess that means you're running in the browser (though it isn't necessarily so). And when you store the XML file, where do you want to store it? (In the file system of the computer where the browser is running? At the server where the page was served from?) – Daniel Earwicker Jul 28 '09 at 7:03
i need t store it on server.. – Avinash Jul 28 '09 at 7:03

AFAIK you can't really create a new xml file with client-side Javascript, usually this is done through a server-side language ( Javascript can be used for server side but it's a rarity ).

Is there any particular reason why you can't go with PHP or Python to create the xml file?

share|improve this answer
yes my main application is running on PHP.. – Avinash Jul 28 '09 at 7:01
1  
If you can retrieve the google map data with server-side, then you can create an xml file. If you can't, with the data from PHP you could make an XHR ( Ajax ) http request and send the data to a server-side page which can then create/modify an xml file. – meder Jul 28 '09 at 7:03
yes i m thinking of doing that now.. – Avinash Jul 28 '09 at 7:04

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.