XML Tutorials - Herong's Tutorial Examples - v5.25, by Herong Yang
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): print(pad+node.nodeName) 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) else: 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 dictionary word update |@date: 2100-01-01 name |@is_acronym: true |#text: DTD definition |#text: Document Type Definition
Notes on the Python script and output:
Table of Contents
Introduction of XML (eXtensible Markup Language)
DOM (Document Object Model) Programming Interface
SAX (Simple API for XML) Programming Interface
DTD (Document Type Definition) Introduction
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
Validating XML Documents Against Specified XML Schemas
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
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 Tools Plugin for Notepad++
XML Plugin Packages for Atom Editor