Use ElementTree:
import xml.etree.ElementTree as ET
tree = ET.parse('Config.xml')
root = tree.getroot()
print(root.findall('.//Log'))
Output:
pawel@pawel-XPS-15-9570:~/test$ python parse_xml.py
[<Element 'Log' at 0x7fb3f2eee9f
Answer from pawelbylina on Stack OverflowGeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
January 12, 2026 - Let’s look at two libraries used for XML parsing in Python. BeautifulSoup is mainly used for HTML parsing, but when combined with the lxml parser, it can also parse XML files efficiently.
Top answer 1 of 2
5
Use ElementTree:
import xml.etree.ElementTree as ET
tree = ET.parse('Config.xml')
root = tree.getroot()
print(root.findall('.//Log'))
Output:
pawel@pawel-XPS-15-9570:~/test$ python parse_xml.py
[<Element 'Log' at 0x7fb3f2eee9f
2 of 2
0
Below:
import xml.etree.ElementTree as ET
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<Automation_Config>
<Path>
<Log>.\SERVER.log</Log>
<Flag_Path>.\Flag</Flag_Path>
<files>.\PO</files>
</Path>
</Automation_Config>'''
root = ET.fromstring(xml)
for idx,log_element in enumerate(root.findall('.//Log')):
print('{}) Log value: {}'.format(idx,log_element.text))
output
0) Log value: .\SERVER.log
A Roadmap to XML Parsers in Python – Real Python
Heh, this would have been so useful for me a week ago, when I needed to parse an XML. Found xmltodict and it served its purpose beautifully - for what I needed at least. More on reddit.com
Reading and writing out an XML file as flat file?
You probably want to write a SAX parser. Look at xml.sax in the stdlib.
More on reddit.comHow to do XML SAX parsing in python using iterparse()?
I think that elem is an Element type, so elem.attrib will be a dictionary of the attributes.
See https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
edit: so, url = elem.attrib.get('href')
More on reddit.comHow would I parse XML that I get through Pycurl?
Parsing any html or xml is easiest with BeautifulSoup. Just remember to specify that's it's XML, or it will default to HTML:
soup = bs4.BeautifulSoup(body, 'xml')More on reddit.com
Videos
29:27
Python XML Parser Tutorial | Read and Write XML in Python | Python ...
38:58
Python XML Parsing Tutorial Read And Write XML Files In Python ...
Parse XML Files with Python - Basics in 10 Minutes
35:04
Parsing XML files with Python (xml.etree.ElementTree) - YouTube
17:34
Full XML Processing Guide in Python - YouTube
11:20
How to Read XML File in Python (Step-by-Step for Beginners) - YouTube
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - If not given, the standard XMLParser parser is used. parser must be a subclass of XMLParser and can only use the default TreeBuilder as a target. Returns an iterator providing (event, elem) pairs; it has a root attribute that references the root element of the resulting XML tree once source is fully read. The iterator has the close() method that closes the internal file object if source is a filename.
LearnPython.com
learnpython.com › blog › read-xml-into-python
How to Read XML Files into Python | LearnPython.com
This simple mechanism allows us to store arbitrary hierarchical structures into the XML, even if they include hundreds of elements. In fact, formats such as XLSX use XML under the hood in order to manage all the complexity behind an Excel spreadsheet. (This article shows how to read Excel files in Python directly).
Zyte
zyte.com › learn › a-practical-guide-to-xml-parsing-with-python
A Practical Guide to Python XML Parsing
Whether you're dealing with configuration files, SOAP-based APIs, or large datasets used in enterprise applications, understanding how to parse and manipulate XML is essential. In this guide, we’ll dive into the world of XML parsing using · Python, providing not just basic methods but also advanced techniques like handling XML namespaces, performing XPath queries, and mapping XML data to custom Python objects. By the end, you will have a deep understanding of how to read...
Rdegges
rdegges.com › 2013 › quickly-extract-xml-data-with-python
Randall Degges - Quickly Extract XML Data with Python
Instead of going the LXML route, ... using Python’s built in xml.etree module. It’s got a (relatively) simple interface that makes parsing simple XML documents fairly easy – and since that’s all I needed to do, it seemed like a good solution. If you’re looking for a quick way to extract XML data, read ...
DataCamp
datacamp.com › tutorial › python-xml-elementtree
Python XML Tutorial: Element Tree Parse & Read | DataCamp
December 10, 2024 - Check for common issues like malformed XML, unsupported encodings, or incorrect file paths. Use Python's error handling mechanisms (try-except blocks) to diagnose and manage parsing errors gracefully. ElementTree does not support pretty-printing directly, but you can use xml.dom.minidom to parse the XML string and then use its toprettyxml() method to format the XML for readability.
GitHub
github.com › oxylabs › how-to-parse-xml-in-python
GitHub - oxylabs/how-to-parse-xml-in-python: Follow this in-depth technical tutorial to learn how to parse XML data in Python, what libraries you should use, how to handle invalid XML, and more. · GitHub
Now, let's learn how to parse XML files using these libraries. Fortunately, both minidom and ElementTree provide a built-in function to parse XML files. You can use the parse method of minidom to read XML content from a file.
Author oxylabs
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_xml.html
pandas.read_xml — pandas 3.0.1 documentation - PyData |
Read XML document into a DataFrame object. ... String path, path object (implementing os.PathLike[str]), or file-like object implementing a read() function. The string can be a path. The string can further be a URL. Valid URL schemes include http, ftp, s3, and file.
ScrapingAnt
scrapingant.com › blog › python-parse-xml
How to Parse XML in Python | ScrapingAnt
August 2, 2024 - import codecs with codecs.open("example.xml", "r", encoding="utf-8") as file: content = file.read() This approach helps prevent encoding-related parsing errors, especially when dealing with XML files from various sources. By following these best practices, developers can significantly improve the efficiency, reliability, and performance of XML parsing in Python, even when working with large and complex XML documents.
freeCodeCamp
freecodecamp.org › news › how-to-parse-xml-in-python-without-using-external-libraries
How to Parse XML in Python Without Using External Libraries
November 12, 2025 - The xml.etree.ElementTree module works well enough for most XML parsing needs, and it's always available in Python's standard library. For more advanced XML navigation and selection, you can explore XPath expressions. XPath works well for selecting nodes in an XML document and can be very useful for complex structures. We’ll cover this in another tutorial. Until then, happy parsing! ... I'm a self-taught programmer and a technical writer from India. I enjoy reading, writing, and coding.
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
Unlike other XML libraries, automatic type parsing is available, so f.e. if the schema defines an element to be of type int, the parsed dict will contain also an int value for that element. Moreover the library supports automatic and explicit validation of XML documents against a schema. from xmlschema import XMLSchema, etree_tostring # load a XSD schema file schema = XMLSchema("your_schema.xsd") # validate against the schema schema.validate("your_file.xml") # or schema.is_valid("your_file.xml") # decode a file data = schmema.decode("your_file.xml") # encode to string s = etree_tostring(schema.encode(data))
Apify
blog.apify.com › python-parse-xml
How to parse XML in Python
July 16, 2025 - In this guide, you’ll learn in detail how to parse XML files in Python. Here, we’ll focus on the ElementTree API, a built-in, simple, lightweight XML processor for parsing XML. Additionally, we’ll explore extracting and parsing data from XML files using Beautiful Soup and LXML. ... Let's first understand XML, its structure, and how an XML parser functions to process and read data from XML files.