SoFunction
Updated on 2025-03-01

Python minidom module usage example [DOM write and parse XML]

This article describes the usage of Python minidom module. Share it for your reference, as follows:

1. DOM write XML files

# -*- coding:utf-8 -*-
#!python3
#Import minidomfrom  import minidom
# 1. Create a DOM tree objectdom=()
# 2. Create a root node.  Every time, you have to use a DOM object to create any node.root_node=('root')
# 3. Add root node with DOM object(root_node)
# Create element child nodes with DOM objectsbook_node=('book')
# Add element child nodes with parent node objectroot_node.appendChild(book_node)
# Set the properties of this nodebook_node.setAttribute('price','199')
name_node=('name')
root_node.appendChild(name_node)
# Also use DOM to create text nodes, treat text nodes (text content) as child nodesname_text=('Computer Programming Language 1 Edition')
# Add text nodes with the node object with text added (see the parent node of the text node)name_node.appendChild(name_text)
# Each node object (including the dom object itself) has a method to output XML content, such as: toxml()-- string, toptyxml()--beautify the tree format.try:
  with open('dom_write.xml','w',encoding='UTF-8') as fh:
    # ()The first parameter is the target file object, the second parameter is the indentation format of the root node, and the third parameter is the indentation format of other child nodes.    # The fourth parameter sets the line break format, and the fifth parameter sets the encoding of the xml content.    (fh,indent='',addindent='\t',newl='\n',encoding='UTF-8')
    print('Write to xml OK!')
except Exception as err:
  print('error message:{0}'.format(err))

The generated dom_write.xml file results are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <book price="199"/>
 <name>Computer programming language The1version</name>
</root>

2. DOM parsing XML files

# -*- coding:utf-8 -*-
#!python3
from  import minidom
with open('dom_write.xml','r',encoding='utf8') as fh:
  # parse() gets the DOM object  dom=(fh)
  # Get the root node  root=
  # Node name  print()
  # Node type: 'ELEMENT_NODE', element node; 'TEXT_NODE', text node; 'ATTRIBUTE_NODE', attribute node  print()
  # Get all child nodes under a node, which is a list  print()
  # Get the element node through the dom object or root element according to the label name, which is a list  book=('book')[0]
  # Get node properties  print(('price'))
  # Get the text content of an element node, first get the subtext node, and then get the text content through the "data" attribute  name=('name')[0]
  name_text_node=[0]
  print(name_text_node.data)
  # Get the parent node of a node  print()

Run output:

root
1
[<DOM Text node "'\n\t'">, <DOM Element: book at 0x1dd2800>, <DOM Text node "'\n\t'">, <DOM Element: name at 0x1dd2850>, <DOM Text node "'\n'">]
199
Computer Programming Language 1 Edition
root

PS: Here are a few online tools for your reference:

OnlineXML/JSON mutual conversion tool:
http://tools./code/xmljson

Online formattingXML/Online compressionXML
http://tools./code/xmlformat

XMLOnline compression/formatting tools:
http://tools./code/xml_format_compress

XMLCode online formatting and beautification tool:
http://tools./code/xmlcodeformat

For more information about Python-related content, please check out the topic of this site:Summary of Python's xml data skills》、《Python data structure and algorithm tutorial》、《Summary of Python Socket Programming Tips》、《Summary of Python function usage tips》、《Summary of Python string operation skills》、《Python introduction and advanced classic tutorials"and"Summary of Python file and directory operation skills

I hope this article will be helpful to everyone's Python programming.