While less common for modern applications, Python also supports alternative parsing models:
For large datasets or tasks requiring complex queries, lxml is the industry standard. It is a third-party library that acts as a Pythonic binding for the C libraries libxml2 and libxslt . How to parse xml using python
: You can parse a file directly using ET.parse('file.xml') or a string using ET.fromstring(xml_data) . While less common for modern applications, Python also
: A minimal implementation of the Document Object Model. It is useful if you are already familiar with the DOM API from JavaScript, but it can be memory-intensive as it loads the entire document into RAM. : A minimal implementation of the Document Object Model
import xml.etree.ElementTree as ET # Parsing from a string root = ET.fromstring(' Python Guide ') # Accessing the root tag and attributes print(f"Root: {root.tag}") # Finding specific elements for book in root.findall('book'): title = book.find('title').text print(f"Book ID {book.get('id')}: {title}") Use code with caution. Copied to clipboard 2. High-Performance Parsing: lxml