How to parse an XML file in Python
Use xml.etree.ElementTree to parse XML files efficiently: 1. Use ET.parse() to read the file and get the root element; 2. Use findall and find to traverse elements and extract data; 3. Use ET.fromstring() to parse XML strings; 4. Use .attrib and .text to access attributes and text content.

To parse an XML file in Python, you can use the built-in xml.etree.ElementTree module. It's simple, efficient, and well-suited for most XML parsing tasks.
1. Load and Parse an XML File
Use ElementTree.parse() to load and parse an XML file from disk.
- import xml.etree.ElementTree as ET
- tree = ET.parse('example.xml')
- root = tree.getroot() # Get the root element
The root is an Element object representing the top-level tag in the XML.
2. Navigate and Access Elements
Once you have the root, you can loop through child elements or search using tags.
- # Iterate over child elements
- for child in root:
- print(child.tag, child.attrib)
- # Find all elements with a specific tag
- for elem in root.findall('book'):
- title = elem.find('title').text
- author = elem.find('author').text
- print(title, author)
3. Parse XML from a String
If your XML is in a string rather than a file, use ET.fromstring() .
- xml_data = '''
- Value
''' - root = ET.fromstring(xml_data)
- print(root.find('item').text)
4. Handle Attributes and Text
Elements can have attributes and text content. Access them using .attrib and .text .
- for item in root.findall('book'):
- print("ID:", item.get('id')) # Attribute
- print("Title:", item.find('title').text) # Text inside
Basically that's it — with ElementTree, you can read, search, and extract data from XML efficiently without external dependencies.
The above is the detailed content of How to parse an XML file in Python. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20518
7
13631
4
How to call the pre-trained model in Python_HuggingFace library model download and fine-tuning
Mar 31, 2026 pm 12:42 PM
The main reason for model download failure is network failure or lack of HuggingFace authentication; fine-tuning OOM is because float32 weights are loaded by default, so device_map="auto" should be used first for inference; mixed precision error reporting requires checking the data type and properly configuring fp16/bf16.
Where are Python third-party libraries installed by default_Modify pip's default global installation path
Apr 03, 2026 pm 12:09 PM
pipinstall is installed by default to the site-packages directory corresponding to the current Python interpreter, which is determined by sys.path and is affected by the isolation of the virtual environment (venv/conda/pyenv); a common error is that pip and python point to different environments, causing the import to fail.
Python Flask project structure design_following MVC principles to achieve high code cohesion
Mar 31, 2026 pm 12:24 PM
Model in Flask refers to entity classes and data logic defined by ORM such as SQLAlchemy. It should be independent of views and HTTP contexts, concentrated in the models/ directory, and encapsulate fields, queries and business verification.
Why relative import in Python must first import the package as a module
Apr 03, 2026 pm 01:27 PM
Relative import depends on the __package__ attribute of the module to locate the parent package, but running the script directly (such as pythonproject/one.py) will cause __main__.__package__ to be None, causing . to be unresolved. Only when imported through python-mpackage.module or an external driver script, __package__ is correctly set to the package name, thus enabling relative import.
How to build multiple libraries in Python Django_implementation of high concurrency mechanism based on database routing configuration and master-slave separation of reading and writing
Mar 31, 2026 pm 12:15 PM
Django read-write separation requires customizing the DatabaseRouter class and registering it to DATABASE_ROUTERS. db_for_read must determine transactions to avoid inconsistencies, and db_for_write must return to the main library; select_related cross-database JOIN will be invalid, and prefetch_related or unified model libraries should be used instead; ConnectionDoesNotExist needs to check whether the route return value accurately matches the DATABASES key name; strong consistency reads should actively use='default' instead of relying on retry.
How to run a Python script_Detailed explanation of various ways to run a Python script and command line operations
Apr 03, 2026 pm 01:51 PM
To run a Python script, make sure that Python is installed, the PATH configuration is correct, and the script has no syntax errors; confirm the interpreter path and version through which/where and --version; shebang only takes effect on Linux/macOS and requires chmod x; when reporting module errors, you need to check the working directory, sys.path, piplist, and running mode.
Python FastAPI exception logging_Integrate Sentry to implement monitoring and alarming
Apr 01, 2026 pm 06:18 PM
Sentry needs to check four points if exceptions are not caught in FastAPI: init must be before app=FastAPI() and not within ifname=="__main__":; Sentry middleware must be the outermost layer; asynchronous tasks need to manually capture_exception(); 422 errors need to rewrite exception_handler and report it.
Recommended Python code syntax highlighting library: Pygments practical guide
Apr 03, 2026 pm 01:24 PM
Pygments is a powerful, out-of-the-box universal syntax highlighting library that supports 581 programming languages and text formats. It can be directly integrated into Python desktop chat applications and automatically generates colored code snippets (such as ANSI, HTML or Qt text formats), completely replacing the rough highlighting solution of manual regular matching.





