O'Reilly logo

HTML5 Data and Services Cookbook by Mite Mitreski, Gorgi Kosev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required