Friday, January 20, 2012

Creating XML document using JavaScript

DOM Leavel 2 provides a document.implementation.createDocument method which can be used to create a blank XML document using the following syntax:

var doc = document.implementation.createDocument(namespaceuri, root, doctype);

We can use this API in the following way:

var doc = document.implementation.createDocument("","root", null);
var node = doc.createElement("node");
doc.documentElement.appendChild(node);

This creates an XML DOM document with no default namespace and no doctype.

Browser Support:

  • Internet Explorer 9+
  • Firefox
  • Opera
  • Chrome
  • Safari

1 comment:

codeismydrug said...

Hi;

Thank you for this clear and concise post.

I've been sorting through tutorials and lengthy "look at how smart I am!" dissertations on different document types and things I don't need to know, when all I needed was the syntax.