The DOMParser interface provides the ability to parse XML or HTML source code from a string into a DOM Document.

Note: XMLHttpRequest can parse XML and HTML directly from a URL-addressable resource, returning a Document in its response property.

You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the XMLSerializer interface.

In the case of an HTML document, you can also replace portions of the DOM with new DOM trees built from HTML by setting the value of the Element.innerHTML and outerHTML properties. These properties can also be read to fetch HTML fragments corresponding to the corresponding DOM subtree.

Creating a DOMParser

To create a DOMParser object simply use new DOMParser().

Parsing XML

Once you have created a parser object, you can parse XML from a string using the parseFromString() method:

var parser = new DOMParser();
var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");

Error handling

Note that if the parsing process fails, the DOMParser does not throw an exception, but instead returns an error document:

<parsererror xmlns="">
(error description)
<sourcetext>(a snippet of the source XML)</sourcetext>

The parsing errors are also reported to the Error Console, with the document URI (see below) as the source of the error.

Parsing an SVG or HTML document

The DOMParser can also be used to parse a SVG document (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) or an HTML document (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9). There are three different results possible, selected by the MIME type given. If the MIME type is text/xml, the resulting object will be an XMLDocument, if the MIME type is image/svg+xml, it will be an SVGDocument and if the MIME type is text/html, it will be an HTMLDocument.

var parser = new DOMParser();
var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
// returns a Document, but not a SVGDocument nor a HTMLDocument

parser = new DOMParser();
doc = parser.parseFromString(stringContainingSVGSource, "image/svg+xml");
// returns a SVGDocument, which also is a Document.

parser = new DOMParser();
doc = parser.parseFromString(stringContainingHTMLSource, "text/html");
// returns a HTMLDocument, which also is a Document.

DOMParser HTML extension for other browsers

 * DOMParser HTML extension
 * 2012-09-04
 * By Eli Grey,
 * Public domain.

/*! @source */
/*global document, DOMParser*/

(function(DOMParser) {
	"use strict";

	var proto = DOMParser.prototype, 
        nativeParse = proto.parseFromString;

	// Firefox/Opera/IE throw errors on unsupported types
	try {
		// WebKit returns null on unsupported types
		if ((new DOMParser()).parseFromString("", "text/html")) {
			// text/html parsing is natively supported
	} catch (ex) {}

	proto.parseFromString = function(markup, type) {
		if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
			  doc = document.implementation.createHTMLDocument("")
	      		if (markup.toLowerCase().indexOf('<!doctype') > -1) {
        			doc.documentElement.innerHTML = markup;
      			else {
        			doc.body.innerHTML = markup;
			return doc;
		} else {
			return nativeParse.apply(this, arguments);


Specification Status Comment
DOM Parsing and Serialization
The definition of 'DOMParser' in that specification.
Working Draft Initial definition

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support1 Yes1983.2
DOMParser() constructor1 Yes1983.2
parseFromString1 Yes1983.2
parseFromString: XML (application/xml) support1 Yes1983.2
parseFromString: SVG (image/svg+xml) support4 Yes1010153.2
parseFromString: HTML (text/html) support30 Yes1210179.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes ? Yes Yes Yes ? ?
DOMParser() constructor Yes ? Yes Yes Yes ? ?
parseFromString Yes ? Yes Yes Yes ? ?
parseFromString: XML (application/xml) support Yes ? Yes Yes Yes ? ?
parseFromString: SVG (image/svg+xml) support ? ? Yes10 Yes ? ?
parseFromString: HTML (text/html) support ? ? Yes14 ? ? ?

See also