The XML DOM (Document Object Model) is a programming interface for XML documents. It defines the structure of the XML document as a tree of objects that can be accessed, modified, deleted, or added to using programming languages like JavaScript, Python, or Java. The XML DOM is essential for interacting with XML data in a structured and programmable way.
<book>
).id="001"
).<title>
).parentNode
, childNodes
, firstChild
, lastChild
, nextSibling
, and previousSibling
.Before you can manipulate an XML document with the DOM, you need to load it into memory.
import xml.dom.minidom
# Load the XML document
dom_tree = xml.dom.minidom.parse("books.xml")
root_element = dom_tree.documentElement
Here, dom_tree
is the DOM representation of the XML document, and root_element
is the root of the XML tree (e.g., <bookstore>
).
You can access specific elements and their attributes using the DOM API.
XML Developer's Guide
Author Name
29.99
Learning XML
Another Author
39.95
# Get all book elements
books = root_element.getElementsByTagName("book")
for book in books:
title = book.getElementsByTagName("title")[0].childNodes[0].data
author = book.getElementsByTagName("author")[0].childNodes[0].data
price = book.getElementsByTagName("price")[0].childNodes[0].data
book_id = book.getAttribute("id")
print(f"Book ID: {book_id}, Title: {title}, Author: {author}, Price: {price}")
// Output //
Book ID: 001, Title: XML Developer's Guide, Author: Author Name, Price: 29.99
Book ID: 002, Title: Learning XML, Author: Another Author, Price: 39.95
You can also modify the content of elements and their attributes.
# Change the price of the first book
books[0].getElementsByTagName("price")[0].childNodes[0].data = "24.99"
# Change the id attribute of the second book
books[1].setAttribute("id", "003")
# Save the changes back to the XML file
with open("updated_books.xml", "w") as file:
dom_tree.writexml(file)
After running this code, the first book’s price will be updated to “24.99”, and the second book’s id
attribute will change from “002” to “003”.
You can add new elements or remove existing ones from the XML document.
# Create a new book element
new_book = dom_tree.createElement("book")
new_book.setAttribute("id", "004")
# Create and append title, author, and price elements
new_title = dom_tree.createElement("title")
new_title.appendChild(dom_tree.createTextNode("Mastering XML"))
new_book.appendChild(new_title)
new_author = dom_tree.createElement("author")
new_author.appendChild(dom_tree.createTextNode("Expert Author"))
new_book.appendChild(new_author)
new_price = dom_tree.createElement("price")
new_price.appendChild(dom_tree.createTextNode("49.99"))
new_book.appendChild(new_price)
# Append the new book to the bookstore
root_element.appendChild(new_book)
# Save the updated XML
with open("updated_books.xml", "w") as file:
dom_tree.writexml(file)
# Remove the first book element
root_element.removeChild(books[0])
# Save the updated XML
with open("updated_books.xml", "w") as file:
dom_tree.writexml(file)
After running these examples, a new book is added to the XML document, and the first book is removed.
You can clone (copy) and import nodes between different XML documents.
# Clone the second book element
cloned_book = books[1].cloneNode(True) # True means deep clone
# Append the cloned book to the bookstore
root_element.appendChild(cloned_book)
# Save the updated XML
with open("cloned_books.xml", "w") as file:
dom_tree.writexml(file)
If your XML document uses namespaces, you can access elements and attributes using their namespace URIs.
# Suppose the XML uses namespaces, like xmlns:bk="http://example.com/books"
# You can access elements by their namespace URI and local name
books = root_element.getElementsByTagNameNS("http://example.com/books", "book")
for book in books:
title = book.getElementsByTagNameNS("http://example.com/books", "title")[0].childNodes[0].data
print(f"Title: {title}")
The XML DOM (Document Object Model) provides a powerful way to interact with and manipulate XML documents programmatically. By representing the XML data as a tree of objects, the DOM allows you to access, modify, add, or delete elements and attributes in a flexible and structured manner. Happy coding !❤️