How to create xml file with jquery

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

+4
source share
6 answers

Not with JavaScript browser, no. You will need some kind of server to write to the file system for you. You can always create a file in JS and then send it via AJAX so that the server can write.

+4
source

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 the usual jQuery DOM manipulation to add nodes to this XML document. After you need to serialize the final document, you can use the answers to this question .

+4
source

Your question is more complex than it would be at first. Let me break it into two parts:

  • Is it possible to create a file using javascript?
  • Is it possible to write XML to this file using jQuery / javascript?

Unfortunately, the answer to the first question is no. You cannot use javascript in a browser to access the file system. This has security implications and will not be permitted by major browsers.

The answer to the first question makes the second controversial. You can create XML in javascript, but you have nowhere to write.

If you provide more information about the reason you want to do this, you may be able to find alternative solutions to your problem.

+2
source

How to create it just using javascript strings and then using this library?

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

0
source

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 line:

 new XMLSerializer().serializeToString(doc); 

But it will be only in memory. What else do you want to do with this?

0
source

Seeing me learning XML myself is probably an illegal answer because it contains a possible question.

However, I am sure that the XML document can be sent to the browser for display. This can be explicitly saved by the end user.

0
source

All Articles