---
description: Learn how to create,read, and parse XML Files in Python using minidom class and ElementTree. Python XML Tutorial with Example.
title: Python XML File &#8211; How to Read, Write &#038; Parse
image: https://www.guru99.com/images/python-xml-file.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Python XML processing lets you read, write, and parse XML documents using the built-in minidom and ElementTree modules. The minidom class loads a file into memory as a DOM, while ElementTree offers a faster, more Pythonic tree API.

- 🔘 **What is XML:** eXtensible Markup Language stores and transports small, structured data.
- ☑️ **minidom parsing:** The parse() function loads an XML file into memory as a DOM.
- ✅ **Reading tags:** getElementsByTagName() returns matching elements; getAttribute() reads their values.
- 🧪 **Writing nodes:** createElement() and appendChild() add a new tag to a document.
- 🛠️ **ElementTree API:** ET.parse() builds a tree; getroot() returns the root element.
- 🤖 **AI ready:** Parsed XML feeds configs and API data into machine learning pipelines.

Read More

![Python XML File](https://www.guru99.com/images/python-xml-file.png)

The sections below cover parsing, writing, and reading XML files in Python.

## What is XML?

XML stands for eXtensible Markup Language. It was designed to store and transport small to medium amounts of data and is widely used for sharing structured information.

Python enables you to parse and modify XML documents. In order to parse an XML document, you need to have the entire XML document in memory. In this tutorial, we will see how we can use the XML minidom class in Python to load and parse XML files.

## How to Parse XML using minidom

We have created a sample XML file that we are going to parse.

**Step 1) Create Sample XML file**

Inside the file, we can see the first name, last name, home, and the area of expertise (SQL, Python, Testing and Business).

[![How to Parse XML using minidom]()](https://www.guru99.com/images/Pythonnew/Python20.1.png)

**Step 2) Use the parse function to load and parse the XML file**

Once we have parsed the document, we will print out the “node name” of the root of the document and the “firstchild tagname”. Tagname and nodename are the standard properties of the XML file.

[![Parse XML using minidom]()](https://www.guru99.com/images/Pythonnew/Python20.2.png)

- Import the xml.dom.minidom module and declare the file that has to be parsed (myxml.xml)
- This file carries some basic information about an employee like first name, last name, home, expertise, etc.
- We use the parse function on the XML minidom to load and parse the XML file
- We have a variable doc, and doc gets the result of the parse function
- We want to print the nodename and child tagname from the file, so we declare it in the print function
- Run the code- It prints out the nodename (#document) from the XML file and the first child tagname (employee) from the XML file

**Note**: Nodename and child tagname are the standard names or properties of an XML dom.

**Step 3) Call the list of XML tags from the XML document and print it**

Next, we can also call the list of XML tags from the XML document and print it. Here we printed out the set of skills like SQL, Python, [Testing](https://www.guru99.com/software-testing.html) and Business.

[![Parse XML using minidom]()](https://www.guru99.com/images/Pythonnew/Python20.3.png)

- Declare the variable expertise, from which we are going to extract all the expertise names the employee has
- Use the dom standard function called “getElementsByTagName”
- This will get all the elements named skill
- Declare a loop over each one of the skill tags
- Run the code- It will give a list of four skills

## How to Write XML Node

We can create a new attribute by using the “createElement” function and then append this new attribute or tag to the existing XML tags. We added a new tag “BigData” in our XML file.

1. You have to write code to add the new attribute (BigData) to the existing XML tag
2. Then, you have to print out the XML tag with the new attribute appended to the existing XML tag

[![Write XML Node](<> "Manipulating XML with Python")](https://www.guru99.com/images/Pythonnew/Python20.4.png)

- To add a new XML tag and add it to the document, we use the code “doc.createElement”
- This code will create a new skill tag for our new attribute “BigData”
- Add this skill tag into the document’s first child (employee)
- Run the code- the new tag “BigData” will appear with the other list of expertise

**Don't Miss:**

- [How to Find Average of List in Python](https://www.guru99.com/find-average-list-python.html)
- [Python time.sleep(): Add Delay to Your Code (Example)](https://www.guru99.com/python-time-sleep-delay.html)
- [Python Matrix: Transpose, Multiply, NumPy Arrays](https://www.guru99.com/python-matrix.html)
- [Polymorphism in Python with EXAMPLES](https://www.guru99.com/polymorphism-in-python.html)

### XML Parser Example

**Python 2 Example**

```
import xml.dom.minidom

def main():
# use the parse() function to load and parse an XML file
   doc = xml.dom.minidom.parse("Myxml.xml");
  
# print out the document node and the name of the first child tag
   print doc.nodeName
   print doc.firstChild.tagName
  
# get a list of XML tags from the document and print each one
   expertise = doc.getElementsByTagName("expertise")
   print "%d expertise:" % expertise.length
   for skill in expertise:
     print skill.getAttribute("name")
    
#Write a new XML tag and add it into the document
   newexpertise = doc.createElement("expertise")
   newexpertise.setAttribute("name", "BigData")
   doc.firstChild.appendChild(newexpertise)
   print " "

   expertise = doc.getElementsByTagName("expertise")
   print "%d expertise:" % expertise.length
   for skill in expertise:
     print skill.getAttribute("name")
    
if name == "__main__":
  main();
```

**Python 3 Example**

```
import xml.dom.minidom

def main():
    # use the parse() function to load and parse an XML file
    doc = xml.dom.minidom.parse("Myxml.xml");

    # print out the document node and the name of the first child tag
    print (doc.nodeName)
    print (doc.firstChild.tagName)
    # get a list of XML tags from the document and print each one
    expertise = doc.getElementsByTagName("expertise")
    print ("%d expertise:" % expertise.length)
    for skill in expertise:
        print (skill.getAttribute("name"))

    # Write a new XML tag and add it into the document
    newexpertise = doc.createElement("expertise")
    newexpertise.setAttribute("name", "BigData")
    doc.firstChild.appendChild(newexpertise)
    print (" ")

    expertise = doc.getElementsByTagName("expertise")
    print ("%d expertise:" % expertise.length)
    for skill in expertise:
        print (skill.getAttribute("name"))

if __name__ == "__main__":
    main();
```

## How to Parse XML using ElementTree

ElementTree is an API for manipulating XML. ElementTree is an easy way to process XML files.

We are using the following XML document as the sample data:

```
<data>
   <items>
      <item name="expertise1">SQL</item>
      <item name="expertise2">Python</item>
   </items>
</data>

```

**Reading XML using ElementTree:**

We must first import the xml.etree.ElementTree module.

```
import xml.etree.ElementTree as ET

```

Now let us fetch the root element:

```
root = tree.getroot()

```

Following is the complete code for reading the above XML data:

```
import xml.etree.ElementTree as ET
tree = ET.parse('items.xml')
root = tree.getroot()

# all items data
print('Expertise Data:')

for elem in root:
   for subelem in elem:
      print(subelem.text)

```

**Output:**

```
Expertise Data:
SQL
Python

```

## FAQs

🆚 minidom or ElementTree — which XML parser should I use?

Use minidom for small files needing DOM navigation. Choose ElementTree for most work — it is faster, lighter, and the recommended, more Pythonic default.

💾 How do I save an XML tree to a file in Python?

Wrap the root in an ElementTree and call tree.write(“out.xml”, encoding=”utf-8″, xml\_declaration=True). For indentation, use ET.indent() on Python 3.9+, or lxml’s pretty\_print.

📦 What is lxml and how does it differ from ElementTree?

lxml is a third-party library sharing the ElementTree API but backed by fast C code. It adds XPath, XSLT, and validation. Install via pip install lxml.

🔒 How do I parse untrusted XML safely in Python?

Standard parsers are vulnerable to XXE and billion-laughs attacks. Install defusedxml and import defusedxml.ElementTree — a drop-in replacement that disables dangerous external entities.

🧵 Can I parse an XML string instead of a file?

Yes. Use minidom.parseString(text) or ElementTree’s ET.fromstring(text), which returns the root directly. Both parse XML from a variable, so responses need not be saved first.

🔄 How do I convert XML into a Python dictionary or JSON?

Walk the tree with ElementTree, building a dict from each element’s tag, attributes, and text. The xmltodict library automates conversion via xmltodict.parse().

🤖 How is XML parsing used in AI and machine learning workflows?

XML stores configs, annotations, and API responses that feed models. Pascal VOC image labels ship as XML; ElementTree extracts their bounding boxes for training.

🧠 Can GitHub Copilot help write Python XML parsing code?

Yes. GitHub Copilot autocompletes minidom and ElementTree patterns like parse() and findall() loops. Still verify tag names and encodings, and add defusedxml for untrusted input.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-xml-file.png","url":"https://www.guru99.com/images/python-xml-file.png","width":"700","height":"250","caption":"Python XML File","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/manipulating-xml-with-python.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/manipulating-xml-with-python.html","name":"Python XML File &#8211; How to Read, Write &#038; Parse"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/manipulating-xml-with-python.html#webpage","url":"https://www.guru99.com/manipulating-xml-with-python.html","name":"Python XML File &#8211; How to Read, Write &#038; Parse","dateModified":"2026-10-07T15:00:15+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-xml-file.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/manipulating-xml-with-python.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python XML File &#8211; How to Read, Write &#038; Parse","description":"Learn how to create,read, and parse XML Files in Python using minidom class and ElementTree. Python XML Tutorial with Example.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T15:00:15+05:30","image":{"@id":"https://www.guru99.com/images/python-xml-file.png"},"copyrightYear":"2026","name":"Python XML File &#8211; How to Read, Write &#038; Parse","subjectOf":[{"@type":"HowTo","name":"How to Parse XML using minidom","description":"We have created a sample XML file that we are going to parse.","step":[{"@type":"HowToStep","name":"Step 1) Create Sample XML file","text":"Inside the file, we can see the first name, last name, home, and the area of expertise (SQL, Python, Testing and Business)","image":"https://www.guru99.com/images/Pythonnew/Python20.1.png","url":"https://www.guru99.com/manipulating-xml-with-python.html#step1"},{"@type":"HowToStep","name":"Step 2) Use the parse function to load and parse the XML file","text":"Once we have parsed the document, we will print out the 'node name' of the root of the document and the 'firstchild tagname'.","image":"https://www.guru99.com/images/Pythonnew/Python20.2.png","url":"https://www.guru99.com/manipulating-xml-with-python.html#step2"},{"@type":"HowToStep","name":"Step 3) Call the list of XML tags from the XML document and printed out","text":"We can also call the list of XML tags from the XML document and printed out. Here we printed out the set of skills like SQL, Python, Testing and Business.","image":"https://www.guru99.com/images/Pythonnew/Python20.3.png","url":"https://www.guru99.com/manipulating-xml-with-python.html#step3"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"minidom or ElementTree \u2014 which XML parser should I use?","acceptedAnswer":{"@type":"Answer","text":"Use minidom for small files needing DOM navigation. Choose ElementTree for most work \u2014 it is faster, lighter, and the recommended, more Pythonic default."}},{"@type":"Question","name":"How do I save an XML tree to a file in Python?","acceptedAnswer":{"@type":"Answer","text":"Wrap the root in an ElementTree and call tree.write(\"out.xml\", encoding=\"utf-8\", xml_declaration=True). For indentation, use ET.indent() on Python 3.9+, or lxml's pretty_print."}},{"@type":"Question","name":"What is lxml and how does it differ from ElementTree?","acceptedAnswer":{"@type":"Answer","text":"lxml is a third-party library sharing the ElementTree API but backed by fast C code. It adds XPath, XSLT, and validation. Install via pip install lxml."}},{"@type":"Question","name":"How do I parse untrusted XML safely in Python?","acceptedAnswer":{"@type":"Answer","text":"Standard parsers are vulnerable to XXE and billion-laughs attacks. Install defusedxml and import defusedxml.ElementTree \u2014 a drop-in replacement that disables dangerous external entities."}},{"@type":"Question","name":"Can I parse an XML string instead of a file?","acceptedAnswer":{"@type":"Answer","text":"Yes. Use minidom.parseString(text) or ElementTree's ET.fromstring(text), which returns the root directly. Both parse XML from a variable, so responses need not be saved first."}},{"@type":"Question","name":"How do I convert XML into a Python dictionary or JSON?","acceptedAnswer":{"@type":"Answer","text":"Walk the tree with ElementTree, building a dict from each element's tag, attributes, and text. The xmltodict library automates conversion via xmltodict.parse()."}},{"@type":"Question","name":"How is XML parsing used in AI and machine learning workflows?","acceptedAnswer":{"@type":"Answer","text":"XML stores configs, annotations, and API responses that feed models. Pascal VOC image labels ship as XML; ElementTree extracts their bounding boxes for training."}},{"@type":"Question","name":"Can GitHub Copilot help write Python XML parsing code?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot autocompletes minidom and ElementTree patterns like parse() and findall() loops. Still verify tag names and encodings, and add defusedxml for untrusted input."}}]}],"@id":"https://www.guru99.com/manipulating-xml-with-python.html#schema-213763","isPartOf":{"@id":"https://www.guru99.com/manipulating-xml-with-python.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/manipulating-xml-with-python.html#webpage"}}]}
```
