How to Write XML File in Python: Simple Guide
To write an XML file in Python, use the
xml.etree.ElementTree module to create elements and then write them to a file with ElementTree.write(). This method builds the XML structure in memory and saves it as a file.Syntax
Use xml.etree.ElementTree to create XML elements and build a tree. Then use ElementTree.write(filename) to save the XML to a file.
Element(tag): Creates an XML element with the given tag.SubElement(parent, tag): Creates a child element under a parent.ElementTree(element): Wraps the root element to manage the tree.write(filename): Saves the XML tree to a file.
python
import xml.etree.ElementTree as ET root = ET.Element('root') # Create root element child = ET.SubElement(root, 'child') # Create child element child.text = 'Hello XML' # Add text to child tree = ET.ElementTree(root) # Create tree from root tree.write('output.xml', encoding='utf-8', xml_declaration=True) # Write XML to file
Example
This example creates a simple XML file with a root and two child elements, then writes it to example.xml. It shows how to add tags and text content.
python
import xml.etree.ElementTree as ET # Create root element root = ET.Element('library') # Add first book element book1 = ET.SubElement(root, 'book') book1.set('id', '1') title1 = ET.SubElement(book1, 'title') title1.text = 'Python Basics' author1 = ET.SubElement(book1, 'author') author1.text = 'Alice' # Add second book element book2 = ET.SubElement(root, 'book') book2.set('id', '2') title2 = ET.SubElement(book2, 'title') title2.text = 'Learning XML' author2 = ET.SubElement(book2, 'author') author2.text = 'Bob' # Create tree and write to file tree = ET.ElementTree(root) tree.write('example.xml', encoding='utf-8', xml_declaration=True)
Common Pitfalls
Common mistakes when writing XML files in Python include:
- Not setting
encodingandxml_declaration=Trueinwrite(), which can cause encoding issues or missing XML headers. - Forgetting to add text content to elements, resulting in empty tags.
- Not using
SubElementproperly, which can break the XML structure. - Trying to write the tree before building the full structure.
python
import xml.etree.ElementTree as ET # Wrong: Missing encoding and declaration root = ET.Element('root') child = ET.SubElement(root, 'child') child.text = 'data' tree = ET.ElementTree(root) tree.write('bad.xml') # No encoding or XML declaration # Right: Add encoding and declaration tree.write('good.xml', encoding='utf-8', xml_declaration=True)
Quick Reference
Summary tips for writing XML files in Python:
- Use
ElementandSubElementto build XML structure. - Set element text with
element.text = 'your text'. - Use
ElementTree.write(filename, encoding='utf-8', xml_declaration=True)to save properly. - Check the XML file after writing to ensure correct format.
Key Takeaways
Use xml.etree.ElementTree to create and write XML files in Python.
Always include encoding and xml_declaration when writing to file.
Build the full XML tree before writing to avoid incomplete files.
Use SubElement to add child elements properly under a parent.
Set text content on elements to include data inside tags.