"""Offline XML DOM verification lab for Chapter 09 (nodes-info)."""
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><!-- sample comment -->
<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>
</bookstore>"""
def case_node_name_by_kind():
doc = minidom.parseString(BOOKS_XML)
root = doc.documentElement
book0 = doc.getElementsByTagName("book")[0]
attr = book0.getAttributeNode("category")
text = doc.getElementsByTagName("title")[0].childNodes[0]
comment = [n for n in root.childNodes if n.nodeType == n.COMMENT_NODE][0]
expected = {
"element": "bookstore",
"attribute": "category",
"text": "#text",
"comment": "#comment",
"document": "#document",
}
actual = {
"element": root.nodeName,
"attribute": attr.nodeName,
"text": text.nodeName,
"comment": comment.nodeName,
"document": doc.nodeName,
}
return {
"name": "node_name_by_kind",
"summary": "验证元素、属性、文本、注释与文档节点的只读 nodeName 命名规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_node_value_read_and_write():
doc = minidom.parseString(BOOKS_XML)
title_el = doc.getElementsByTagName("title")[0]
book0 = doc.getElementsByTagName("book")[0]
text_node = title_el.childNodes[0]
before = text_node.nodeValue
text_node.nodeValue = "Easy Cooking"
after = title_el.childNodes[0].nodeValue
expected = {
"element_node_value_is_none": True,
"attr_node_value": "cooking",
"before_text": "Everyday Italian",
"after_text": "Easy Cooking",
}
actual = {
"element_node_value_is_none": title_el.nodeValue is None,
"attr_node_value": book0.getAttributeNode("category").nodeValue,
"before_text": before,
"after_text": after,
}
return {
"name": "node_value_read_and_write",
"summary": "核对元素 nodeValue 为空(W3C null)、属性与文本 nodeValue 读取,以及将首书标题改写为 Easy Cooking",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_node_type_constants_table():
doc = minidom.parseString(BOOKS_XML)
root = doc.documentElement
book0 = doc.getElementsByTagName("book")[0]
attr = book0.getAttributeNode("category")
text = doc.getElementsByTagName("title")[0].childNodes[0]
comment = [n for n in root.childNodes if n.nodeType == n.COMMENT_NODE][0]
expected = {"元素": 1, "属性": 2, "文本": 3, "注释": 8, "文档": 9}
actual = {
"元素": root.nodeType,
"属性": attr.nodeType,
"文本": text.nodeType,
"注释": comment.nodeType,
"文档": doc.nodeType,
}
return {
"name": "node_type_constants_table",
"summary": "核对常用 5 种 DOM 节点类型的只读 nodeType 整数常量(1/2/3/8/9)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_lxml_verify_modified_title():
doc = minidom.parseString(BOOKS_XML)
doc.getElementsByTagName("title")[0].childNodes[0].nodeValue = "Easy Cooking"
root = etree.fromstring(doc.toxml().encode("utf-8"))
expected = {"first_title": "Easy Cooking", "second_title": "Harry Potter"}
actual = {
"first_title": root.findtext("book[1]/title"),
"second_title": root.findtext("book[2]/title"),
}
return {
"name": "lxml_verify_modified_title",
"summary": "将修改 nodeValue 后的文档序列化并用 lxml.etree 重新解析验证",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_node_name_by_kind(),
case_node_value_read_and_write(),
case_node_type_constants_table(),
case_lxml_verify_modified_title(),
]
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-info-lab/explore-nodes-info.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-nodes-info.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a2752.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2776.zip