1. Using xml.etree.ElementTree
(Built-in Library)
xml.etree.ElementTree
is a simple and efficient built-in library for parsing and creating XML data. It’s useful for handling most basic XML parsing needs.
Example: Parsing XML with Element Tree
import xml.etree.Element Tree as ET
# Sample XML string
xml_data = '''
<bookstore>
<book>
<title lang="en">Python Programming</title>
<author>John Doe</author>
<price>29.95</price>
</book>
<book>
<title lang="fr">Programmation Python</title>
<author>Jean Dupont</author>
<price>35.99</price>
</book>
</bookstore>
'''
# Parse the XML string
root = ET.fromstring(xml_data)
# Accessing elements
for book in root.findall('book'):
title = book.find('title').text
author = book.find('author').text
price = book.find('price').text
print(f'Title: {title}, Author: {author}, Price: {price}')
Output:
Title: Python Programming, Author: John Doe, Price: 29.95
Title: Programmation Python, Author: Jean Dupont, Price: 35.99
Key Functions in Element Tree
:
ET.fromstring()
: Parses an XML string.root.findall('tag')
: Finds all elements with the specified tag.element.find('tag')
: Finds the first element with the specified tag.element.text
: Gets the text inside an XML element.
🔹 2. Using lxml
(External Library)
lxml
is a third-party library that is more feature-rich and supports XPath, XSLT, and schema validation. It is generally faster and more versatile than ElementTree
, especially for large or complex XML documents.
To use lxml
, you’ll need to install it first:
Example: Parsing XML with lxml
from lxml import etree
# Sample XML string
xml_data = '''
<bookstore>
<book>
<title lang="en">Python Programming</title>
<author>John Doe</author>
<price>29.95</price>
</book>
<book>
<title lang="fr">Programmation Python</title>
<author>Jean Dupont</author>
<price>35.99</price>
</book>
</bookstore>
'''
# Parse the XML string
root = etree.fromstring(xml_data)
# Accessing elements
for book in root.xpath('//book'):
title = book.xpath('title/text()')[0]
author = book.xpath('author/text()')[0]
price = book.xpath('price/text()')[0]
print(f'Title: {title}, Author: {author}, Price: {price}')
Output:
Title: Python Programming, Author: John Doe, Price: 29.95
Title: Programmation Python, Author: Jean Dupont, Price: 35.99
Key Functions in lxml
:
etree.fromstring()
: Parses an XML string.element.xpath()
: Executes an XPath query to find elements or values.
🔹 3. Using minidom
(Built-in Library)
xml.dom.minidom
is a part of the Python standard library and provides a way to parse XML and access it through the DOM (Document Object Model).
Example: Parsing XML with minidom
from xml.dom import minidom
# Sample XML string
xml_data = '''
<bookstore>
<book>
<title lang="en">Python Programming</title>
<author>John Doe</author>
<price>29.95</price>
</book>
<book>
<title lang="fr">Programmation Python</title>
<author>Jean Dupont</author>
<price>35.99</price>
</book>
</bookstore>
'''
# Parse the XML string
dom = minidom.parseString(xml_data)
# Accessing elements
books = dom.getElementsByTagName('book')
for book in books:
title = book.getElementsByTagName('title')[0].firstChild.data
author = book.getElementsByTagName('author')[0].firstChild.data
price = book.getElementsByTagName('price')[0].firstChild.data
print(f'Title: {title}, Author: {author}, Price: {price}')
Output:
Title: Python Programming, Author: John Doe, Price: 29.95
Title: Programmation Python, Author: Jean Dupont, Price: 35.99
🔹 Summary of Methods
Method | Library | Usage | Pros |
---|---|---|---|
ElementTree |
Built-in (Standard) | Lightweight, easy-to-use | Simple and fast for small XML files |
lxml |
Third-party (lxml ) |
XPath, advanced features | Powerful, faster for large XML files |
minidom |
Built-in (Standard) | DOM-based, tree-like structure | Good for small XML, but slower |
🔹 Conclusion
ElementTree
is ideal for most use cases due to its simplicity and speed.lxml
is great when you need XPath support or need to work with very large XML files efficiently.minidom
can be used if you prefer working with the DOM structure directly, but it may be slower compared toElementTree
.
Would you like more examples with XML file parsing or advanced XPath queries?
Leave a comment