MENU
String Loading with DOMParser
An XML string already present in the page – for example inside a <script type="text/xmldata"> block – can be parsed into a DOM document with DOMParser.parseFromString(), without any network request. The resulting document can be modified like any other DOM document, then turned back into a string with XMLSerializer.serializeToString().Using a nonstandard <xml> tag causes the browser to implicitly display the XML data it contains.
<!DOCTYPE html>
<html>
<head>
<script id="x1" type="text/xmldata">
<a><b>Hello <c/> <d>World</d></b>
<b>!</b></a>
</script>
<xml id="x2">
<a><b>Hello <c/> <d>World</d></b>
<b>!</b></a>
</xml>
<script>
Xstring = document.getElementById("x1").innerHTML;
parser = new DOMParser();
XML = parser.parseFromString(Xstring,"text/xml");
b2 = XML.createElement("b");
b2.textContent = "Hello Earth!";
root = XML.getElementsByTagName("a")[0];
root.appendChild(b2);
serializer = new XMLSerializer();
alert(serializer.serializeToString(root));
</script>
</head>
<body></body>
</html>