Parsing and serializing XML

The Web platform provides the following objects for parsing and serializing XML:

  • XMLSerializer to serialize DOM trees to strings
  • XPath to address (point to) different parts of an XML document (through a non-XML syntax) into strings
  • DOMParser to parse XML from strings into DOM trees
  • XMLHttpRequest to parse URL-addressable resources into DOM trees

Part 1: How to create an XML document

Using one of the following approaches to create an XML document (which is an instance of Document).

Parsing strings into DOM trees

var sMyString = '<a id="a"><b id="b">hey!</b></a>';
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(sMyString, "text/xml");
// print the name of the root element or error message
dump(oDOM.documentElement.nodeName == "parsererror" ? "error while parsing" : oDOM.documentElement.nodeName);

Building an XML document starting from a JavaScript Object tree (JXON)

Please see the JXON reverse algorithms.

Parsing URL-addressable resources into DOM trees

Using XMLHttpRequest

Here is sample code that reads and parses a URL-addressable XML file into a DOM tree:

var xhr = new XMLHttpRequest();
xhr.onload = function() {
xhr.onerror = function() {
  dump("Error while getting XML.");
}"GET", "example.xml");
xhr.responseType = "document";

xhr.responseXML is a Document instance.

Part 2: How to serialize the content of a given XML document

Use the following approaches to serialize the contents of the XML document you created in Part 1.

Serializing DOM trees to strings

First, create a DOM tree as described in How to Create a DOM tree. Alternatively, use a DOM tree obtained from XMLHttpRequest.

Now, let's serialize doc — the DOM tree — to a string:

var oSerializer = new XMLSerializer();
var sXML = oSerializer.serializeToString(doc);

The new XMLSerializer() constructor is not available from within a JS XPCOM component (or a JS module). Instead, write:

var oSerializer = Components.classes[";1"]
var sXML = oSerializer.serializeToString(doc);

"Pretty" serialization of DOM trees to strings

You can pretty print a DOM tree using XMLSerializer and E4X. First, create a DOM tree as described in the How to Create a DOM tree article. Alternatively, use a DOM tree obtained from XMLHttpRequest. The doc variable contains the DOM tree.

var oSerializer = new XMLSerializer();
var sPrettyXML = XML(oSerializer.serializeToString(doc)).toXMLString();

Indents consist of two spaces. To write a more efficient version or customize the indent string, use treeWalker.

Note: When using the E4X toXMLString method, your CDATA elements will be lost, and only the containing text will remain. So if you have CDATA elements in your XML, using the preceding method might not be useful.
<content><![CDATA[This is the content]]></content>


<content>This is the content</content>

Serializing DOM trees to Javascript Object trees (JXON)

JXON (lossless JavaScript XML Object Notation) is a way to represent JavaScript Objects using XML. To address only parts of an XML document, use XPath instead of converting the whole document into JSON! Otherwise, read the article about JXON.

Serializing DOM trees to files

First, create a DOM tree as described in the How to Create a DOM tree article. If you already have a DOM tree from using XMLHttpRequest, skip to the end of this section.

Now, let's serialize doc, the DOM tree, to a file. For more information about files, see about using files in Mozilla):

var oFOStream = Components.classes[";1"].createInstance(Components.interfaces.nsIFileOutputStream);
var oFile = Components.classes[";1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile); // get profile folder
oFile.append("extensions"); // extensions sub-directory
oFile.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}"); // GUID of your extension
oFile.append("myXMLFile.xml"); // filename
oFOStream.init(oFile, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
(new XMLSerializer()).serializeToStream(doc, oFOStream, ""); // rememeber, doc is the DOM tree

Serializing XMLHttpRequest objects to files

If you already have a DOM tree from using XMLHttpRequest, use the same code as above but replace serializer.serializeToStream(doc, oFOStream, "") with serializer.serializeToStream(xmlHttpRequest.responseXML.documentElement, oFOStream, "") where xmlHttpRequest is an instance of XMLHttpRequest.

Note that this first parses the XML retrieved from the server, and then re-serializes it into a stream. Depending on your needs, you could just save the xmlHttpRequest.responseText directly.

Serializing HTML documents

If the DOM you have is an HTML document, you can serialize it simply using

var serialized = document.documentElement.innerHTML;

See also