Skip to content Skip to sidebar Skip to footer

How To Create Xml File With Jquery

Is there a way to create xml file only with jquery/javascript ?

Solution 1:

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.

Solution 2:

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.

Solution 3:

Your question is more complicated than it would first seem. Let's break it down into two parts:

  1. Can I create a file using javascript?
  2. Can I write XML to that file using jQuery/javascript?

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.

Solution 4:

How about creating it by just using Javascript strings and then using this library?

http://plugins.jquery.com/project/createXMLDocument

Solution 5:

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?

Post a Comment for "How To Create Xml File With Jquery"