Skip to content

XML DOM

How to access and manipulate XML documents using the Document Object Model (XML DOM) with standard tree-based methods and JavaScript examples.

DOM stands for Document Object Model and XML DOM defines a standard way to access and manipulate XML documents.

What is the DOM?

According to W3C documentation:

The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

So, we can say that HTML DOM and XML DOM are similar. In facts, both defines a standard way to access and manipulate documents through a tree structure.

Example of HTML DOM

All HTML elements can be accessed through the HTML DOM.

For example

<!DOCTYPE html>
<html>
	<body>
		<h1 id="changeme">a H1 Heading</h1>
		<button type="button" onclick="document.getElementById('changeme').innerHTML='Hello World!'">Click!
		</button>
	</body>
</html>

Example of XML DOM

All XML elements can be accessed through the XML DOM.

<?xml version="1.0"  encoding="UTF-8"?>
<people>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
</people>

This code retrieves the text value of the first <author> element in an XML document:

txt = xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;
Navigation