MDN wants to learn about developers like you:

XMLSerializer can be used to convert a DOM subtree or DOM document into text. XMLSerializer is available to unprivileged scripts.


For more information about using XMLSerializer in Firefox extensions, please see the documentation for nsIDOMSerializer.


Returns the serialized subtree of a string.
The subtree rooted by the specified element is serialized to a byte stream using the character set specified.


 var s = new XMLSerializer();
 var d = document;
 var str = s.serializeToString(d);

The next example uses XMLSerializer with insertAdjacentHTML() to insert a newly created DOM Node into the Document's body. Because insertAdjacentHTML() accepts a string and not a Node for its second parameter, XMLSerializer is used to first convert the node into a string.

var inp = document.createElement('input');
var XMLS = new XMLSerializer(); 
var inp_xmls = XMLS.serializeToString(inp); // First convert DOM node into a string

// Insert a newly created node into the document's body
document.body.insertAdjacentHTML('afterbegin', inp_xmls);

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) (Yes) 9.0 (Yes) 3.0.4
serializeToStream() No support No support Removed in 20.0 (20.0) No support No support No support
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) (Yes) ? ? ?
serializeToStream() No support No support Removed in 20.0 (20.0) No support No support No support

See also

Document Tags and Contributors

 Last updated by: erikadoyle,