Reading XML in Python

Reading XML in Python involves parsing and extracting the desired information from the XML data. Python provides several libraries to work with XML data strings, including the built-in xml.etree.ElementTree, xml.dom.minidom, and third-party libraries like xmltodict and lxml. In this Python Read XML example, we are reading and parsing XML string using xml.etree.ElementTree library. You can find more examples of reading XML in Python below. Click Execute to run the Python Read XML Example online and see the result.
Reading XML in Python Execute
import xml.etree.ElementTree as ET

xml_data = '''
<User>
    <Name>Alice</Name>
    <Status>admin</Status>
</User>
'''

root = ET.fromstring(xml_data)

status = root.find('Status').text

print(f"Status: {status}")
Updated: Viewed: 532 times

What is Python?

Python is a high-level, interpreted programming language known for its simplicity, readability, and extensive library support. Python is used for web development, data analysis, artificial intelligence, and automation, among other fields. Python is widely used for reading data in various formats, including XML and data parse.

What is XML?

XML, or Extensible Markup Language, is a versatile tool used in various digital arenas such as web development, data storage, and network communications due to its inherent flexibility and scalability. XML unified and self-descriptive nature allows it to act as a universal language that facilitates the exchange of data between heterogeneous systems. XML is especially popular in multi-platform environments because it is compatible with many programming languages, including Python, for which libraries such as lxml and xml.etree.ElementTree offer rich XML processing capabilities.

Python Read XML Examples

The following are examples of reading XML in Python:

Read XML using xml.etree.ElementTree

The xml.etree.ElementTree module is part of the Python standard library and provides a simple and efficient way to read and manipulate XML data. An event-driven XML parser processes XML data sequentially and fires events when it encounters elements, attributes, and content in an XML document.

Read XML with ElementTree Example
import xml.etree.ElementTree as ET

xml_data = '''
<root>
  <person>
    <name>Jack</name>
    <age>28</age>
  </person>
  <person>
    <name>Leo</name>
    <age>26</age>
  </person>
</root>
'''

tree = ET.fromstring(xml_data)

for person in tree.findall('person'):
    name = person.find('name').text
    age = person.find('age').text
    print(f"Name: {name}, Age: {age}")

# output: 
# Name: Jack, Age: 28
# Name: Leo, Age: 26

Read XML using xml.dom.minidom

The minidom module allows you to transform XML documents into a tree structure called the Document Object Model (DOM). While not as efficient as ElementTree, it provides easy navigation through XML data.

Read XML with DOM Example
import xml.dom.minidom as minidom

xml_data = '''
<User>
    <Name>Alice</Name>
    <Status>admin</Status>
</User>
'''

dom = minidom.parseString(xml_data)
user = dom.getElementsByTagName('User')[0]

name = user.getElementsByTagName('Name')[0].childNodes[0].data
status = user.getElementsByTagName('Status')[0].childNodes[0].data

print(f"Name: {name}, Status: {status}")

# output: Name: Alice, Status: admin

Read XML using lxml

To use the "lxml" library for parsing XML in Python, you need to install it first. You can install it using pip:

Install lxml Example
pip install lxml

The xmltodict library allows to easily convert XML data into a Python dictionary. The xmltodict.parse() function converts XML data into a Python dictionary.

Read XML with lxml Example
from lxml import etree

xml_data = '''
<User>
    <Name>Alice</Name>
    <Status>admin</Status>
</User>
'''

root = etree.fromstring(xml_data)

name_element = root.find('Name')
status_element = root.find('Status')

name = name_element.text if name_element is not None else None
status = status_element.text if status_element is not None else None

print(f"Name: {name}, Status: {status}")

# output: Name: Alice, Status: admin

See also