How to create xml file with jquery - javascript

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

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.

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.

Your question is more complicated than it would first seem. Let's break it down into two parts:
Can I create a file using javascript?
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.

How about creating it by just using Javascript strings and then using this library?
http://plugins.jquery.com/project/createXMLDocument

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?

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.

Related

Javascript Object sync with XML

I have a web app which will do the majority of its work in browser javascript. When the app loads I need to deserialze an XML document and convert the xml elements into javascript objects. I need to then poll this XML document at regular intervals to keep my objects up to date.
My question is...is there a framework I could use to help me here? I'd like to be able to do this intelligently, like not have to ditch all the objects I have in memory each time I poll the XML file. Only get the new ones...or changed ones etc. Thanks.
Something like Breeze? http://www.breezejs.com/
A MVC style Javascript Framework would be a possibility.
Example: http://javascriptmvc.com/

Parsing XML in a Web Worker

I have been using a DOMParser object to parse a text string to an XML tree. However it is not available in the context of a Web Worker (and neither is, of course, document.ELEMENT_NODE or the various other constants that would be needed). Is there any other way to do that?
Please note that I do not want to manipulate the DOM of the current page. The XML file won't contain HTML elements or anything of the sort. In fact, I do not want to touch the document object at all. I simply want to provide a text string like the following:
<car color="blue"><driver/></car>
...and get back a suitable tree structure and a way to traverse it. I also do not care about schema validation or anything fancy. I know about XML for <SCRIPT>, which many may find useful (hence I'm linking to it here), however its licensing is not really suitable for me. I'm not sure if jQuery includes an XML parser (I'm fairly new to this stuff), but even if it does (and it is usable inside a Worker), I would not include an extra ~50K lines of code just for this function.
I suppose I could write a simple XML parser in JavaScript, I'm just wondering if I'm missing a quicker option.
according to the spec
The DOM APIs (Node objects, Document objects, etc) are not available to workers in this version of this specification.
I guess thats why DOMParser is not availlable, but I don't really understand why that decision was made. (fetching and processing an XML document in a WebWorker does not seems unreasonnable)
but you can import other tools available: a "Cross Platform XML Parsing in JavaScript"
At this point I like to share my parser: https://github.com/tobiasnickel/tXml
with its tXml() method you can parse a string into an object and it takes only 0.5kb minified + gzipped

javascript update via external xml or txt possible?

I have a javascript countdown. The first line is:
dateFuture1 = new Date(2011,8,21,20,30,0);
is it possible to update the date and time values via external text file or XML feed and if so, what would it look like?
Would something simple as
2011,8,21,20,30,0
inside a text file be possible?
Thank you in advance
You would need some method of accessing the values within the text or XML file, but yes it is possible. I would suggest looking into the ajax() function within jQuery as it is fully capable of handling your request. You can also utilize JSON to help pass values to the client from the server, which in my opinion is the best way to go.

xml "reusable" code

Is it possible to make a reusable xml code with some javascript inside? like an import or include. My problem is that I need to write different xml files and are almost all the same, what change is just some properties. Any best practice?
Thanks
Pietro
If you know any Java you could use Groovy, a Domain Specific Language on top of Java which has some very fancy XML creating/parsing features. This would allow you to read your list of properties directly from a file.
http://groovy.codehaus.org/Processing+XML
Can you elaborate what you mean by change in properties? A best practice that you are looking for might be to run the xml through an xslt processor to change the value of the properties. Is this feasible in your situation?

Importing external json files to a javascript script client-side without external libraries

I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?
You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.
EDIT:
If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.
Yes there is. You can use the "document.write" to add scripts to the DOM at runtime:
in your case:
document.write('<script ...></script>');
Basically you are adding the script tag to the dom that will request the new file.
However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:
var x = { //json object };

Categories

Resources