xml.dom.minidom for Parsing XML Document

This section provides a tutorial example on how to parse an existing XML document into DOM object tree with Python xml.dom.minidom package.

xml.dom.minidom sub-package offers the following functionalities to parse an existing XML document into a DOM object tree and access its elements/attributes.

Here is an example Python script that parses and browses an XML document using the xml.dom.minidom sub-package:

#- minidom_Parse_XML.py
#- Copyright (c) 2018 HerongYang.com. All Rights Reserved.
import xml.dom.minidom

#- Browse a node recursively 
def browseNode(node, pad):

  map = node.attributes
  for i in range(map.length):
    print(pad+" |@"+map.item(i).name+": "+map.item(i).value)

  list = node.childNodes
  for child in list:
    if child.nodeType == xml.dom.minidom.Element.ELEMENT_NODE:
      browseNode(child, pad+" ")
    elif child.nodeType == xml.dom.minidom.Element.TEXT_NODE:
      text = child.nodeValue.strip()
      if len(text)>0:
        print(pad+" |"+child.nodeName+": "+text)
      print("Something went wrong!")

#- Parse an existing XML file into a DOM document
doc = xml.dom.minidom.parse("dictionary.xml")
root = doc.documentElement

browseNode(root, "")

Use the program from the last tutorial minidom_Build_XML.py to create an XML file. Then parse it with minidom_Parse_XML.py:

herong> python minidom_Build_XML.py > dictionary.xml 

herong> python minidom_Parse_XML.py

   |@date: 2100-01-01
   |@is_acronym: true
   |#text: DTD
   |#text: Document Type Definition

Notes on the Python script and output:

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

Processing XML with Python Scripts

 What Is the Python "xml" Package

 xml.dom.minidom for Building XML Document

xml.dom.minidom for Parsing XML Document

 xml.sax for Parsing XML Document

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials


 Full Version in PDF/EPUB