4

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

emboss
  • 38,880
  • 7
  • 101
  • 108
flopifal
  • 53
  • 1
  • 1
  • 4

6 Answers6

4

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.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • 1
    Ok so no client side, but with server side javascript ? Any ideas how, or with witch library can i do it ? – flopifal Aug 22 '11 at 19:50
  • 1
    "Server side javascript" doesn't exist. If you want to generate the XML in JavaScript, you'll have to do string building, or you could try exploiting the DOM for XML. If you want to generate it server side and are using PHP, check out SimpleXML. http://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch – Alex Turpin Aug 22 '11 at 20:00
  • 1
    I don't mean clean javascript for serverside, i mean javascript with framework for serverside. I want to create a file in client directory, that he can open, and the file to be with ext XML. – flopifal Aug 22 '11 at 20:19
  • 1
    I'm not sure I understand what you want. JavaScript in itself cannot interact with files. If you want to create a file on your server using JavaScript, you will need to use a server-side language like PHP or .NET and call it through AJAX. – Alex Turpin Aug 22 '11 at 20:21
  • 3
    Server-side JavaScript most certainly does exist and has for a very long time. That being said, I think OP is just asking how to create an XML file based on some interaction with web page and is providing too little information. – jiggy Aug 23 '11 at 00:18
4

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.

Community
  • 1
  • 1
emboss
  • 38,880
  • 7
  • 101
  • 108
  • 1
    It is creating a new XML document from scratch. But you can't write it to the file system, though, if you are in a browser environment. But you will be able to submit it to the server where it can be persisted. – emboss Aug 22 '11 at 19:54
  • 1
    I like the idea, but this has a quotation mismatch – Amish Programmer Nov 29 '11 at 17:05
2

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.

Amish Programmer
  • 2,051
  • 3
  • 19
  • 22
0

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

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

slandau
  • 23,528
  • 42
  • 122
  • 184
0

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?

jiggy
  • 3,828
  • 1
  • 25
  • 40
  • so, this will create me a file ? that for example i can open in some directory or etc ? Or it will only convert string to valid xml – flopifal Aug 22 '11 at 19:49
  • 2
    It creates a document in memory, not a file. There is no way for JavaScript running a browser to write a file to the file system. – jiggy Aug 22 '11 at 20:34
0

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.

DaCentaur
  • 1
  • 1