4

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

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok so no client side, but with server side javascript ? Any ideas how, or with witch library can i do it ?
"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. stackoverflow.com/questions/143122/…
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.
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.
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.
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.

2 Comments

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.
I like the idea, but this has a quotation mismatch
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.

Comments

0

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

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

1 Comment

this is not creating the file, this is making string to valid xml
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?

2 Comments

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
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.