Python中xml.dom.minidom库使用方法及多子节点XML文件读取方案咨询
Hey there! Let's work through that "dom not recognized" error you're hitting with xml.dom.minidom, plus cover the right way to use this library and some better alternatives for reading complex XML files in Python.
Fixing the "dom not recognized" Error with xml.dom.minidom
The most common cause of this error is either missing the proper import or misreferencing the library/object. Here's how to use it correctly:
Step 1: Import the library properly
You can import the full module or just the functions you need:
# Option 1: Import the full module import xml.dom.minidom # Option 2: Import specific functions for brevity from xml.dom.minidom import parse, parseString
Step 2: Read and parse your XML file
Here's a complete example that reads a sample XML file and extracts data:
Suppose you have an XML file library.xml like this:
<library> <book> <title>Python Crash Course</title> <author>Eric Matthes</author> </book> <book> <title>Learning Python</title> <author>Mark Lutz</author> </book> </library>
Use this code to parse it:
# Using the full module import dom = xml.dom.minidom.parse("library.xml") root = dom.documentElement # Get the root <library> node # Get all <book> child nodes books = root.getElementsByTagName("book") for book in books: # Extract text from <title> and <author> title = book.getElementsByTagName("title")[0].childNodes[0].nodeValue author = book.getElementsByTagName("author")[0].childNodes[0].nodeValue print(f"Book: {title} by {author}")
Common pitfalls to avoid
- Don't forget to import the library first! If you skip the import line, Python won't recognize
xml.dom.minidomor thedomvariable you create. - Watch for typos: If you accidentally name the variable
Dom(capital D) instead ofdom, you'll get an "unrecognized" error. - Double-check your file path: If the XML file isn't in your working directory, use the full absolute path (e.g.,
/home/user/documents/library.xml).
Better Alternatives for Reading XML
While xml.dom.minidom works, it's a bit verbose and outdated for most use cases. Here are two better options:
1. xml.etree.ElementTree (Built-in, Lightweight)
This is Python's recommended built-in library for XML processing—it's more intuitive and requires less code than minidom.
Example with the same library.xml:
import xml.etree.ElementTree as ET # Parse the XML file tree = ET.parse("library.xml") root = tree.getroot() # Traverse and extract data for book in root.findall("book"): title = book.find("title").text author = book.find("author").text print(f"Book: {title} by {author}")
2. lxml (Third-party, Powerful)
For more complex XML tasks (like XPath queries, schema validation, or handling large files), lxml is the industry standard. It's faster and more feature-rich than the built-in libraries.
First install it via pip:
pip install lxml
Example using XPath to find specific books:
from lxml import etree # Parse the XML file tree = etree.parse("library.xml") root = tree.getroot() # Use XPath to find books by a specific author matches = root.xpath("//book[author='Eric Matthes']") for book in matches: print(f"Found: {book.find('title').text}")
内容的提问来源于stack exchange,提问作者SarkisF

