Reading XML documents with DOMParser

While XMLHttpRequest allows us to both download and parse XML documents, sometimes we might want to parse XML data documents manually. For example, manual parsing would enable us to include arbitrary XML data (for example, XML-based templates) inside the page in a script tag. This can help to reduce the number of requests sent to the browser.

In this recipe, we are going to read a simple XML document from a textarea input and parse it using DOMParser, and then display the result as a tree.

How to do it...

Let's write the test HTML page and the parser:

  1. Create index.html, it should contain a textarea element to input XML (a sample XML document is included), a placeholder for the document body object, and some CSS ...

Get HTML5 Data and Services Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.