"""Offline XML DOM verification lab for Chapter 14 (nodes-get)."""
import json
import sys
import xml.dom.minidom as minidom
from lxml import etree
VERSION = f"CPython {sys.version.split()[0]} + xml.dom.minidom + lxml {etree.__version__}"
BOOKS_XML = """<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>"""
def case_get_first_title_text_node_value():
doc = minidom.parseString(BOOKS_XML)
x = doc.getElementsByTagName("title")[0]
y = x.childNodes[0]
txt = y.nodeValue
all_titles = [el.childNodes[0].nodeValue for el in doc.getElementsByTagName("title")]
expected = {
"first_title": "Everyday Italian",
"title_child_count": 1,
"all_titles": ["Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"],
}
actual = {
"first_title": txt,
"title_child_count": len(x.childNodes),
"all_titles": all_titles,
}
return {
"name": "get_first_title_text_node_value",
"summary": "分步通过 getElementsByTagName、childNodes[0] 与 nodeValue 读取首个及全部 title 文本值",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_get_attribute_method():
doc = minidom.parseString(BOOKS_XML)
first_lang = doc.getElementsByTagName("title")[0].getAttribute("lang")
categories = [b.getAttribute("category") for b in doc.getElementsByTagName("book")]
lxml_cats = [b.get("category") for b in etree.fromstring(BOOKS_XML.encode("utf-8")).findall("book")]
expected = {
"first_title_lang": "en",
"book_categories": ["cooking", "children", "web", "web"],
"matches_lxml": True,
}
actual = {
"first_title_lang": first_lang,
"book_categories": categories,
"matches_lxml": categories == lxml_cats,
}
return {
"name": "get_attribute_method",
"summary": "使用 getAttribute() 获取首书 title 的 lang 属性及全部 book 的 category 属性,并用 lxml 核对",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_get_attribute_node_method():
doc = minidom.parseString(BOOKS_XML)
lang_node = doc.getElementsByTagName("title")[0].getAttributeNode("lang")
cat_values = [b.getAttributeNode("category").nodeValue for b in doc.getElementsByTagName("book")]
expected = {
"node_type": 2,
"node_name": "lang",
"node_value": "en",
"all_categories": ["cooking", "children", "web", "web"],
}
actual = {
"node_type": lang_node.nodeType,
"node_name": lang_node.nodeName,
"node_value": lang_node.nodeValue,
"all_categories": cat_values,
}
return {
"name": "get_attribute_node_method",
"summary": "使用 getAttributeNode() 返回属性节点对象并读取其 nodeName/nodeType/nodeValue",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_missing_attribute_boundary_behavior():
doc = minidom.parseString(BOOKS_XML)
book0 = doc.getElementsByTagName("book")[0]
expected = {
"has_cover": False,
"minidom_get_attribute_missing": "",
"get_attribute_node_missing_is_none": True,
}
actual = {
"has_cover": book0.hasAttribute("cover"),
"minidom_get_attribute_missing": book0.getAttribute("cover"),
"get_attribute_node_missing_is_none": book0.getAttributeNode("cover") is None,
}
return {
"name": "missing_attribute_boundary_behavior",
"summary": "验证缺失属性时 hasAttribute、getAttribute 与 getAttributeNode 的边界返回结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_get_first_title_text_node_value(),
case_get_attribute_method(),
case_get_attribute_node_method(),
case_missing_attribute_boundary_behavior(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。
入口文件:projects/dom-nodes-get-lab/explore-nodes-get.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-nodes-get.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2775.zip
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a2752.zip