Using JavaScript To Create an XML File From Scratch

svet-am

Supreme [H]ardness
Joined
Jan 6, 2003
Messages
5,146
I am back with yet another n00b JS DOM question. I finally got all of my XML _parsing_ done (at least I hope so) and now I'm working on code to create an XML object from scratch.

What is the correct way to create an XML document from scratch that I can then send to a server for processing?

I found
Code:
    if(window.DOMParser){
        xmlDoc = new DOMParser();
        
    }else{
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        
    }

Is that correct?
 
thought I'd follow-up on this in case anyone else stumbles on to it. what I found out was that there _is_ a way to create an actual, parsable XML DOM object from scratch but it's not trivial and it's not consistent across browser implementations.

the easiest thing I found (since most applications are just creating XML for submission to a server like me) is to just concatenate all of the XML into a normal JavaScript string and then submit that string to the server.
 
thought I'd follow-up on this in case anyone else stumbles on to it. what I found out was that there _is_ a way to create an actual, parsable XML DOM object from scratch but it's not trivial and it's not consistent across browser implementations.

Are you talking about cases like this:

Code:
var markup = "<root><test>test</test></root>";
var xmldoc = (new DOMParser()).parseFromString(markup, "application/xml");
alert(xmldoc);
alert((new XMLSerializer()).serializeToString(xmldoc));
var xmldoc2 = document.implementation.createDocument(null, "root", null);
alert(xmldoc2);
var test = xmldoc2.documentElement.appendChild(xmldoc2.createElement("test"));
test.textContent = "test";
alert((new XMLSerializer()).serializeToString(xmldoc2));

where Opera adds an XML declaration even if there isn't one? Or, is something else inconsistent?
 
from my research, _nothing_ is consistent. Some browsers expect you to do something like "new DOMParser()" where others expect you to do something like "new XMLHTTPRequest()" -- and then there's IE, which has a completely different set of concerns about how to do it depending on which revision.

Then, assuming you get one created, some create a documentElement for you and some don't -- it's actually quite a mess.
 
Hmm, the example I posted works the same in IE9, Chrome, Opera, Firefox and Safari except for the serializeToString() difference in Opera with the implicit xml declaration.

For XHR, the following works in Opera, Safari, Chrome and Firefox:

Code:
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
        alert(this.responseXML);
    }
};
req.open("GET", "doc.xml", true);
req.send();

and there shouldn't be any problems as long as the xml doc is valid and sent as application/xml.

This one will make it work in IE9:

Code:
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
        alert((new DOMParser()).parseFromString(this.responseText, "application/xml"));
    }
};
req.open("GET", "doc.xml", true);
req.send();

So, support seems to be pretty good across browsers. But yeh, if you're dealing with a bunch or different versions of IE, you might have fun. In that case JQuery could help with that as it abstracts all the xhr problems away.

the easiest thing I found (since most applications are just creating XML for submission to a server like me) is to just concatenate all of the XML into a normal JavaScript string and then submit that string to the server.

Understood of course. Just wanted to FYI ya just in case you missed some things.
 
Last edited:
You could use JSONP to pass an object back and parse/convert the rest on the server side.

Should be quite a bit easier.
 
Shadow2531 -- your examples all open an existing XML file from document. I need to create a completely from-scratch XML node tree that will _never_ be written to a file. It was just going to be submitted to the server for processing. This is where the existing mechanisms break down.
 
Shadow2531 -- your examples all open an existing XML file from document. I need to create a completely from-scratch XML node tree that will _never_ be written to a file. It was just going to be submitted to the server for processing. This is where the existing mechanisms break down.

The document.implementation.createDocument method that was in my example creates an XML document from scratch.
 
Back
Top