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 OverflowPython
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.
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
Can Python read/write XML and Excel XLSX files?
I’ve been doing tutorials about ... be making. User goes to a specific URL, one for each specific program. For example, one URL will be to read business card XML files and spit out...... More on discuss.python.org
Code to read and parse XML data
Good Morning, I have several thousand XML files that i need to parse and process. To that end I was reviewing a possible solution How to get an XML element text in a LibreOffice Basic macro? - Stack Overflow Rather than use OLE Automation, I prefer to use native libraries provide by LO. More on ask.libreoffice.org
Read xml column inside csv file with Python
Hi, I am very new in Python and need to read an xml column inside a csv file with Python. I can see some code on google on how to read that csv file but I don’t know how to read the xml column inside the csv file. Can someone help? any idea would be appreciated. Thank you! More on discuss.python.org
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
Videos
38:58
Python XML Parsing Tutorial Read And Write XML Files In Python ...
Parse XML Files with Python - Basics in 10 Minutes
11:20
How to Read XML File in Python (Step-by-Step for Beginners) - YouTube
17:34
Full XML Processing Guide in Python - YouTube
29:27
Python XML Parser Tutorial | Read and Write XML in Python | Python ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_xml.html
pandas.read_xml — pandas 3.0.1 documentation
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.
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
January 12, 2026 - To modify an XML file, you first parse it, then change attributes or content, and finally save or print it. ... from bs4 import BeautifulSoup with open('dict.xml', 'r') as f: data = f.read() bs_data = BeautifulSoup(data, 'xml') for tag in bs_data.find_all('child', {'name':'Frank'}): tag['test'] = "WHAT !!" print(bs_data.prettify()) ... ElementTree is included in Python’s standard library, so no installation is required.
OneUptime
oneuptime.com › home › blog › how to work with xml files in python
How to Work with XML Files in Python
January 25, 2026 - Python's xml.etree.ElementTree module is included in the standard library and handles most XML tasks well. # xml_reader.py # Reading and parsing XML files with ElementTree import xml.etree.ElementTree as ET def read_xml_file(filepath): """Read and parse an XML file, returning the root element.""" # Parse the XML file and get the tree structure tree = ET.parse(filepath) # Get the root element of the tree root = tree.getroot() return root def extract_book_data(root): """Extract book information from the XML structure.""" books = [] # Find all 'book' elements under the root for book in root.finda
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
import xmltodict with open('path/to/file.xml') as fd: doc = xmltodict.parse(fd.read()) and then you can access elements, attributes, and values like this: doc['mydocument']['@has'] # == u'an attribute' doc['mydocument']['and']['many'] # == [u'elements', u'more elements'] doc['mydocument']['plus']['@a'] # == u'complex' doc['mydocument']['plus']['#text'] # == u'element as well' xmltodict also lets you roundtrip back to XML with the unparse function, has a streaming mode suitable for handling files that don’t fit in memory, and supports XML namespaces.
Real Python
realpython.com › ref › stdlib › xml
xml | Python Standard Library – Real Python
>>> import xml.etree.ElementTree as ET >>> root = ET.fromstring("<data><item>Python</item></data>") >>> root.tag 'data' Parses XML documents from strings, file, and other data sources · Supports both SAX and DOM parsing models · Allows creation and modification of XML documents · Provides an API through ElementTree for common XML tasks · Handles Unicode and namespaces in XML documents · Supports reading and writing XML in both compact and pretty-printed formats ·
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...
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.
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.
Mirketa
mirketa.com › home › blog › xml parsing: using minidom vs element tree (etree) in python
XML Parsing in Python with ElementTree & BeautifulSoup
July 23, 2025 - Explore how to parse and manipulate XML data using Python libraries like ElementTree, BeautifulSoup, and lxml.
MicroPyramid
micropyramid.com › blog › building-and-parsing-xml-document-using-python
Building and Parsing XML Document using Python | MicroPyramid
In addition to parsing XML, xml.etree.ElementTree used for creating well-formed documents from Element objects in an application. The Element class used when a document is parsed also knows how to generate a serialized form of its contents, which can then be written to a file.
AskPython
askpython.com › home › python xml parser
Python XML Parser - AskPython
July 7, 2022 - This will automatically read the XML input file and get the root node for us. ... Okay, so it seems that is has parsed. But we cannot verify it yet. So let’s parse the other attributes and try to get its value. So now, our task is to get the value inside the <heading> attribute, with the use of our Python ...
Python.org
discuss.python.org › python help
Read xml column inside csv file with Python - Python Help - Discussions on Python.org
April 24, 2022 - Hi, I am very new in Python and need to read an xml column inside a csv file with Python. I can see some code on google on how to read that csv file but I don’t know how to read the xml column inside the csv file. Can …