Parsing and Serializing XML

This section will demonstrate parsing and storing XML.

Loading XML

Mozilla provides a number of objects for parsing XML. The object you want to use depends on what you want to do. Since Mozilla supports rendering XML with an associated style sheet, you can load the URL of a file into the browser by typing it into address field. This will cause the XML to be parsed into a DOM tree and displayed in the browser window. XML doesn't have any scripting or style attributes by itself. To add scripts or style attributes, you'll need to use tags from either the XHTML namespace or tags from the XUL namespace. You can also manipulate XML using script from another window, as long as there aren't any cross-domain security issues.

To create a new document, you can use the document.implementation.createDocument() function. This function can be used to create a new document from another document. This is a standard DOM feature and you can add elements to the document using the document.createElement function and various other DOM node insertion methods. Here are some examples of creating a new document:

var doc1 = document.implementation.createDocument("","",null);
var doc2 = document.implementation.createDocument(

The first example creates a default empty document. The second example creates an XHTML document with a root tag of html. Note that this is an XHTML document, not an HTML document. The first argument to the createDocument method is the default namespace of the document and the second argument is the root tag name. If this is not specified, no root tag is added to the document -- you'll have to add it yourself with the appendChild function. The third argument to createDocument is the DOCTYPE for the document. You can set this to null.

Once you have a document, you can add nodes to it using the DOM functions appendChild, insertChildAt and so forth. You can generally manipulate the document as well as style features in the same way that you would manipulate any other document using a script.

Note that there are many document functions that are HTML-specific which XML documents do not implement. The properties and methods of the nsIDOMHTMLDocument and nsIDOMNSHTMLDocument interfaces are only available to HTML documents.

You can load another XML document into a new document by using the document.load function. For example:


By default, URLs are loaded asynchronously, so the load function will return before the URL has loaded. Documents also have a async property which can be set to false in order to load the XML synchronously. In you plan on using the load function to load a document, you may wish to use the first form of createDocument in the example above, by supplying empty arguments, since this information will be determined from the loaded document.

Note that the documents created with createDocument will not be displayed to the user in any way. You would use this method when the XML is data that you plan to examine or manipulate internally. If you wish to display the XML to the user, The URL will need to be loaded in a browser or in a frame. In XUL, you could load an XML file into an iframe element. Change the value of the src attribute in order to load a different document.

Another way to load XML documents is by using the XMLHttpRequest object. The details of this object will be discussed in another section. Briefly, it allows you to load XML from a URI, but also allows you to set HTTP headers and send POST data along with the request.

Parsing XML

Sometimes, you will have a string of XML that you want to parse. For instance, some XML that the user entered. The DOMParser object may be used to parse XML from a string. This object is available in JavaScript without having to use XPCOM, so you can use it in unprivileged web pages. Here is an example:

var parser = new DOMParser();
var doc = parser.parseFromString("<items><item name='Apple'/><items>", "text/xml");

First, use the DOMParser constructor to create a new DOMParser. You can reuse this object to parse as many strings of XML as desired. The parser has a method parseFromString which may be used to parse a string of XML. This function returns a DOM document object so you can use DOM functions to examine or modify it after it has been parsed. It always returns a complete document, never a document fragment. If you want to parse a fragment of XML, you may be able to wrap it in a single tag and then parse it. Although a document will be returned, you will be able to access the children.

The first argument to the parseFromString method is the string to parse. The second argument is the content type of the string. Currently only XML is supported so you should use 'text/xml', 'application/xml' or 'application/xhtml+xml' as the content type.

The DOMParser does not parse HTML, although naturally it will parse XHTML. You could use the innerHTML property of an element to handle non-XML HTML. For instance:

var parent = document.getElementById("listparent");
parent.innerHTML = "<ul><li>This is item 1 <li>This is item 2</ul>";

The above code will parse a string of HTML and append it into an existing element, replacing the existing content of the element. You could hide the element by using a stylesheet if you don't want the content to be visible to the user. To hide an HTML element, set the CSS display property to none. Note that the innerHTML property is only available for HTML elements.

The DOMParser can also be used to parse XUL, since it is an XML document. However, there will be some features such as templates that will only work for XUL documents. For more information, see How do I parse a string of XUL?.

The DOMParser also includes a parseFromStream method which can be used to parse XML from any object which implements the nsIInputStream interface. This method may not be used from unprivileged code, such as that running on a web page.

The parseFromStream method has four arguments for the stream, the character set, the length of the content and the content type. If is slightly more efficient to use this method than parsing a string, but tends to involve more code.

Handling XML Errors

You can check if a XML document is well-formed by typing its URL into the browser's address bar. Well-formed documents are displayed either using associated style sheets, or as is the case with XHTML or XUL, parsed into XHTML or XUL documents and displayed. If no style information is provided or the type of document is not recognized, Mozilla will present the XML in a source tree view. Non well-formed documents are displayed using a yellow background with the error message and the line in the file where the error occurred as the content.

If the string that the DOMParser parses is not well-formed XML, an error is not returned. Instead, an error document is returned. This error document is a well-formed XML document and you could examine it to find the parsing error that occurred. This error document is the same one that would be displayed to the user if a non well-formed document is encountered from a URL typed into the browser's address bar.

When using the DOMParser to parse XML, you can examine the resulting document to check if it is an error document. The root tag of an error document is a tag parsererror in the namespace If the root tag of the parsed document is this tag, you can tell that an error occured. The content of this tag is the error message which can be displayed to the user or logged.

var parser = new DOMParser();
var doc = parser.parseFromString("<item>Hamster<item></item>, "text/xml");
var roottag = doc.documentElement;
if ((roottag.tagName == "parserError") ||
    (roottag.namespaceURI == "")){
  alert("Parsing Error!");

In this example, the XML to parse is not well-formed since the inner item tag is not closed. After parsing, the code gets the root tag which can be retrieved using the document's documentElement property. If the root tag is a parsererror and has the error namespace, an error message is displayed.

Mozilla only does this error handling for non well-formed XML. Mozilla does not support XML validation with either a DTD or a Schema.

Serializing XML

There is an equivalent object to the DOMParser for serializing a document back into XML. This object is the XMLSerializer and is also available to unprivileged code. It can be used to take a document and return the XML string for its nodes. The XMLSerializer can serialize both an entire document or a single node within a document. In both cases, a string of XML is returned. Here is an example:

var serializer = new XMLSerializer();
var xml = serializer.serializeToString(document);

This example will transform the document from where the script is executing into a string of XML. This conversion is handled by the serializeToString method. The argument is the document or node to serialize. As with the DOMParser, the serializer must be created with a constructor first.

As the name implies, the XMLSerializer only serializes XML, not HTML. You can also serialize XUL. Note that you cannot serialize a document that is from a different domain as this will cause a security exception.

Posting XML

To post an XML document to a web site, you can use the XMLHttpRequest object. The will allow you to post the document in a POST request. Here is an example:

var xrequest=new XMLHttpRequest();"POST","",false);

The example above will post the document referenced by 'documentToPost' to the URL ''. The open method is use to open a connection to a remote site. In this case, a POST request is made. The third argument to the open method indicates whether the request is asynchronous. In this example, false is used so the code will wait until the response is received before returning. The send method is used to send the request, where the argument is the body of the message. In the example, we send a document along with the POST request. The document will be serialized into XML and then sent.

If you want to send just a fragment of an XML document, you must first serialize it into XML using the XMLSerializer object, and then pass the resulting XML string to the send method. The send method accepts either an entire document or a string, but it does not accept a fragment or node.

Add a note User Contributed Notes
No comments available

Copyright © 1999 - 2005