Skip to main content

XML DOM Tutorial

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

HTML DOM manipulation is made using JavaScript.

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;

XML, DOM and XML DOM

DOM defines the objects and properties and methods (interface) to access all XML elements. It is separated into 3 different parts

  • Core DOM: standard model for any structured document.
  • XML DOM: standard model for XML documents.
  • HTML DOM: standard model for HTML documents.

XML DOM is a standard object model for XML.

XML documents have a hierarchy of informational units called nodes while DOM is a standard programming interface of describing those nodes and the relationships between them.

XML DOM also provides an API that allows a developer to add, edit, move or remove nodes anywhere in the tree to create an application.

Advantages of XML DOM

The following are the advantages of XML DOM.

  • XML DOM is language and platform independent.
  • XML DOM is traversable. Information in XML DOM is organized in a hierarchy which allows developer to navigate around the hierarchy looking for specific information.
  • XML DOM is modifiable - It is dynamic in nature providing the developer a scope to add, edit, move or remove nodes at any point on the tree.

Disadvantages of XML DOM

  • It consumes more memory (if the XML structure is large) as program written once remains in memory all the time until and unless removed explicitly.
  • Due to the extensive usage of memory, its operational speed, compared to SAX is slower.