Reputation: 53
Is there a way to create xml file only with jquery/javascript ?
Upvotes: 4
Views: 20914
Reputation: 39650
Use jQuery.parseXML to parse a trivial container string:
var xml = '<?xml version="1.0"?><root/>';
var doc = jQuery.parseXML(xml);
Then you can use normal jQuery DOM manipulation to append nodes to that XML document. Once you need to serialize the final document, you can use the answers to this question.
Upvotes: 4
Reputation: 2121
Your question is more complicated than it would first seem. Let's break it down into two parts:
Unfortunately, the answer to the first question is no. You cannot use javascript running in a browser to access the file system. This has security implications and will not be allowed by major browsers.
The answer to the first question makes the second one moot. You can create XML in javascript, but you'll have no place to write it.
If you provide more information about the reason you want to do this, it may be possible to find alternative solutions to your problem.
Upvotes: 2
Reputation: 1
Seeing as I'm learning XML myself, this is perhaps an illegal answer as it contains a possible question.
That said, I'm pretty sure that an XML document can be sent to the browser to display. That can then be explicitly saved by the end-user.
Upvotes: 0
Reputation: 3836
You can create a document like this:
var doc = document.implementation.createDocument(namespace,rootnode,doctype);
doc.documentElement.appendChild(somenode);
And then serialize it to a string:
new XMLSerializer().serializeToString(doc);
But it will only be in memory. What else do you want to do with it?
Upvotes: 0
Reputation: 24102
How about creating it by just using Javascript strings and then using this library?
http://plugins.jquery.com/project/createXMLDocument
Upvotes: 0
Reputation: 47776
Not with browser JavaScript, no. You will need some kind of server to write to the file system for you. You could always build the file in JS and then send it through AJAX for the server to write though.
Upvotes: 4