MDN self-hosted library · e01.org
XMLSerializer / notebook

XMLSerializer

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const s = new XMLSerializer();
const str = s.serializeToString(document);
saveXML(str);
js
const inp = document.createElement("input");
const XMLS = new XMLSerializer();
const inpSerialized = XMLS.serializeToString(inp); // First convert DOM node into a string

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