Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchFor a regular XML file on disk, Python’s built-in xml.etree.ElementTree module is the simplest starting point: call ET.parse(), get the root element, then navigate its children. If the XML is already a string in memory, use ET.fromstring() instead.
Read an XML file with ElementTree
ET.parse() accepts a filename or a file object and returns an ElementTree. Call getroot() to access the document’s root element.
import xml.etree.ElementTree as ET
tree = ET.parse("data.xml")
root = tree.getroot()
for child in root:
print(child.tag, child.attrib)
Each parsed element represents a node in the XML hierarchy. Its tag is the element name, attrib contains its attributes, and child elements can be iterated over. An element’s text content is available through .text.
Extract elements, text, and attributes
Use find() to look for the first matching child and findall() to find matching direct children. Retrieve an attribute with .get() or inspect .attrib.
#1 Best Overall
for record in root.findall("record"):
name = record.get("name")
value_element = record.find("value")
value = value_element.text if value_element is not None else None
print(name, value)
The check for None is important: find() returns None when it cannot find a matching child. Use the actual element names and attributes in your document rather than assuming they are present.
Parse XML text already in memory
If your XML is held in a Python string rather than a file, pass it to fromstring(). Unlike parse(), this returns the root element directly.
Rank #2
import xml.etree.ElementTree as ET
xml_text = "<catalog><item>Book</item></catalog>"
root = ET.fromstring(xml_text)
item = root.find("item")
if item is not None:
print(item.text)
Choose an API for larger or streamed XML
The right interface depends on where the input comes from and how you need to process it. Python documents several XML APIs; these are the main distinctions relevant to reading data:
| Input or need | Interface | What to know |
|---|---|---|
| File path or file object; convenient tree navigation | xml.etree.ElementTree.parse() |
Builds an element tree for navigation. |
| XML text already in memory | xml.etree.ElementTree.fromstring() |
Returns the root element directly. |
| Large file processed with blocking reads | ElementTree.iterparse() |
Processes parsing events as input is read, but parsed elements remain in the tree unless you clear or remove them. |
| Non-blocking input arriving in chunks | XMLPullParser |
Feed data incrementally and retrieve parsing events without relying on a blocking read. |
| A different processing or programming interface is required | xml.dom, xml.dom.minidom, xml.dom.pulldom, or xml.sax |
Python documents these DOM and SAX interfaces alongside ElementTree. |
iterparse() incrementally builds the tree, but that does not by itself free processed elements. When processing records from a large document, clear them or remove processed children as appropriate. The right cleanup pattern depends on the XML structure and should be checked against the memory needs of the application.
Recommended Free Tools
Search XML that uses namespaces
Namespace declarations change how element names are matched. When searching with ElementTree, use the namespace URI declared by the document, either through a namespace mapping or in the expanded {namespace-uri}local-name form. Do not guess the URI: obtain it from the XML’s namespace declarations.
Handle untrusted XML and check Expat
If a file comes from an untrusted or unauthenticated source, a basic parsing example is not a complete security policy. Python’s XML security guidance describes risks to XML-processing systems including denial of service, local-file access, network connections, and firewall circumvention. It also notes that Expat itself does not access local files or create network connections by default.
Python’s documentation warns that Expat versions lower than 2.7.2 may be vulnerable to “billion laughs,” “quadratic blowup,” and “large tokens” attacks, or disproportionate dynamic-memory use. Python may use bundled or system-wide Expat depending on how the interpreter was configured. Check the version in the environment where the program runs:
import pyexpat
print(pyexpat.EXPAT_VERSION)
These security details are version-sensitive; consult the current Python XML vulnerabilities guidance for the target interpreter. Python’s XML overview separately flags decompression-bomb risk for xmlrpc; that warning should not be treated as a claim that every ordinary ElementTree file parse has the same issue.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesQuick Recap
Best Value
Official Python references
- Python 3.14.8 XML processing overview
- ElementTree reference and tutorial
- Python XML security and vulnerability notes
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




