The XMLSerializer
interface provides the serializeToString()
method to construct an XML string representing a [[../../../Glossary/DOM|DOM]] tree.
Methods
serializeToString()
- Returns the serialized subtree of a string.
serializeToStream()
' '- The subtree rooted by the specified element is serialized to a byte stream using the character set specified.
Examples
Serializing XML into a string
The first, basic, example just serializes an entire document into a string containing XML.
var s = new XMLSerializer();
var d = document;
var str = s.serializeToString(d);
saveXML(str);
This involves creating a new XMLSerializer
object, then passing the Document
to be serialized into serializeToString()
, which returns the XML equivalent of the document.
Inserting nodes into a DOM based on XML
This example uses the Element.insertAdjacentHTML()
method to insert a new DOM Node
into the body of the Document
, based on XML created by serializing an Element
object.
Note: In the real world, you should usually instead call importNode()
method to import the new node into the DOM, then call one of the following methods to add the node to the DOM tree:
- The
Document
andElement
methodsappend()
andprepend()
- The
Node.replaceWith()
method (to replace an existing node with the new one) - The
Document.insertAdjacentElement()
andElement.insertAdjacentElement()
methods.
Because insertAdjacentHTML()
accepts a string and not a Node
as 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 the newly created node into the document's body
document.body.insertAdjacentHTML('afterbegin', inp_xmls);
The code creates a new <input>
element by calling Document.createElement()
, then serializes it into XML using serializeToString()
.
Once that's done, insertAdjacentHTML()
is used to insert the <input>
element into the DOM.
Specifications
Specification | Status | Comment |
DOM Parsing and SerializationThe definition of 'XMLSerializer' in that specification. | Working Draft |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
XMLSerializer
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 9 |
Opera
Full support Yes |
Safari
Full support 3 |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
|
Chrome
Full support Yes |
Edge
Full support 79 |
Firefox
No support ? — 20 |
IE
No support No |
Opera
Full support Yes |
Safari
No support No |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
No support ? — 20 |
Opera Android
Full support Yes |
Safari iOS
No support No |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.'
- Experimental. Expect behavior to change in the future.
- Non-standard. Expect poor cross-browser support.'
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.'
- Deprecated. Not for use in new websites.
See also
XMLSerializer by Mozilla Contributors is licensed under CC-BY-SA 2.5.