[ ![Python logo](../_static/py.svg) ](https://www.python.org/)

Theme  Auto  Light  Dark

###  [ Table of Contents ](../contents.html)

  * ` xml.dom.minidom  ` â Minimal DOM implementation 
    * DOM Objects 
    * DOM Example 
    * minidom and the DOM standard 

####  Previous topic

[ ` xml.dom  ` â The Document Object Model API ](xml.dom.html "previous
chapter")

####  Next topic

[ ` xml.dom.pulldom  ` â Support for building partial DOM trees
](xml.dom.pulldom.html "next chapter")

###  This Page

  * [ Report a Bug ](../bugs.html)
  * [ Show Source ](https://github.com/python/cpython/blob/main/Doc/library/xml.dom.minidom.rst)

###  Navigation

  * [ index ](../genindex.html "General Index")
  * [ modules ](../py-modindex.html "Python Module Index") | 
  * [ next ](xml.dom.pulldom.html "xml.dom.pulldom â Support for building partial DOM trees") | 
  * [ previous ](xml.dom.html "xml.dom â The Document Object Model API") | 
  * ![Python logo](../_static/py.svg)
  * [ Python ](https://www.python.org/) » 
  *   *   * [ 3.12.3 Documentation ](../index.html) » 
  * [ The Python Standard Library ](index.html) » 
  * [ Structured Markup Processing Tools ](markup.html) » 
  * [ ` xml.dom.minidom  ` â Minimal DOM implementation ]()
  * | 
  * Theme  Auto  Light  Dark  | 

#  ` xml.dom.minidom  ` â Minimal DOM implementation  Â¶

**Source code:** [ Lib/xml/dom/minidom.py
](https://github.com/python/cpython/tree/3.12/Lib/xml/dom/minidom.py)

* * *

` xml.dom.minidom  ` is a minimal implementation of the Document Object Model
interface, with an API similar to that in other languages. It is intended to
be simpler than the full DOM and also significantly smaller. Users who are not
already proficient with the DOM should consider using the [ `
xml.etree.ElementTree  ` ](xml.etree.elementtree.html#module-
xml.etree.ElementTree "xml.etree.ElementTree: Implementation of the
ElementTree API.") module for their XML processing instead.

Warning

The  ` xml.dom.minidom  ` module is not secure against maliciously constructed
data. If you need to parse untrusted or unauthenticated data see [ XML
vulnerabilities  ](xml.html#xml-vulnerabilities) .

DOM applications typically start by parsing some XML into a DOM. With  `
xml.dom.minidom  ` , this is done through the parse functions:

    
    
    from xml.dom.minidom import parse, parseString
    
    dom1 = parse('c:\\temp\\mydata.xml')  # parse an XML file by name
    
    datasource = open('c:\\temp\\mydata.xml')
    dom2 = parse(datasource)  # parse an open file
    
    dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')
    

The  ` parse()  ` function can take either a filename or an open file object.

xml.dom.minidom.  parse  (  _ filename_or_file  _ , _ parser  =  None  _ , _
bufsize  =  None  _ )  Â¶

    

Return a ` Document  ` from the given input. _filename_or_file_ may be either
a file name, or a file-like object. _parser_ , if given, must be a SAX2 parser
object. This function will change the document handler of the parser and
activate namespace support; other parser configuration (like setting an entity
resolver) must have been done in advance.

If you have XML in a string, you can use the  ` parseString()  ` function
instead:

xml.dom.minidom.  parseString  (  _ string  _ , _ parser  =  None  _ )  Â¶

    

Return a ` Document  ` that represents the _string_ . This method creates an [
` io.StringIO  ` ](io.html#io.StringIO "io.StringIO") object for the string
and passes that on to  ` parse()  ` .

Both functions return a ` Document  ` object representing the content of the
document.

What the  ` parse()  ` and  ` parseString()  ` functions do is connect an XML
parser with a âDOM builderâ that can accept parse events from any SAX
parser and convert them into a DOM tree. The name of the functions are perhaps
misleading, but are easy to grasp when learning the interfaces. The parsing of
the document will be completed before these functions return;