蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线实测核心 DOM 属性、增删查方法及链式读取 title 文本表达式

PYTHONWindows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。explore-methods.py
explore-methods.py126 行
"""Offline XML DOM verification lab for Chapter 07 (methods)."""
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>
</bookstore>"""


def case_core_dom_node_properties():
    doc = minidom.parseString(BOOKS_XML)
    book0 = doc.getElementsByTagName("book")[0]
    title0 = doc.getElementsByTagName("title")[0]
    text0 = title0.childNodes[0]
    expected = {
        "book_node_name": "book",
        "book_node_value_is_none": True,
        "book_parent_name": "bookstore",
        "book_attr_category": "cooking",
        "title_child_count": 1,
        "text_node_value": "Everyday Italian",
    }
    actual = {
        "book_node_name": book0.nodeName,
        "book_node_value_is_none": book0.nodeValue is None,
        "book_parent_name": book0.parentNode.nodeName,
        "book_attr_category": book0.attributes.getNamedItem("category").nodeValue,
        "title_child_count": len(title0.childNodes),
        "text_node_value": text0.nodeValue,
    }
    return {
        "name": "core_dom_node_properties",
        "summary": "验证典型 DOM 属性 nodeName、nodeValue、parentNode、childNodes 与 attributes 的取值",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_core_dom_node_methods():
    doc = minidom.parseString(BOOKS_XML)
    root = doc.documentElement
    initial_count = len(doc.getElementsByTagName("book"))
    new_book = doc.createElement("book")
    new_title = doc.createElement("title")
    new_title.appendChild(doc.createTextNode("DOM Guide"))
    new_book.appendChild(new_title)
    root.appendChild(new_book)
    after_append = len(doc.getElementsByTagName("book"))
    root.removeChild(new_book)
    after_remove = len(doc.getElementsByTagName("book"))
    expected = {"initial_count": 2, "after_append": 3, "after_remove": 2}
    actual = {"initial_count": initial_count, "after_append": after_append, "after_remove": after_remove}
    return {
        "name": "core_dom_node_methods",
        "summary": "验证典型 DOM 方法 getElementsByTagName、appendChild 与 removeChild 的执行效果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_chained_title_text_lookup():
    doc = minidom.parseString(BOOKS_XML)
    txt = doc.getElementsByTagName("title")[0].childNodes[0].nodeValue
    expected = {"txt": "Everyday Italian"}
    actual = {"txt": txt}
    return {
        "name": "chained_title_text_lookup",
        "summary": "执行链式调用 getElementsByTagName('title')[0].childNodes[0].nodeValue 取得首本书名",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_lxml_verify_mutation_roundtrip():
    doc = minidom.parseString(BOOKS_XML)
    book0 = doc.getElementsByTagName("book")[0]
    edition = doc.createElement("edition")
    edition.appendChild(doc.createTextNode("1st"))
    book0.appendChild(edition)
    lxml_root = etree.fromstring(doc.toxml().encode("utf-8"))
    expected = {"edition_text": "1st", "book_count": 2}
    actual = {
        "edition_text": lxml_root.findtext("book[1]/edition"),
        "book_count": len(lxml_root.findall("book")),
    }
    return {
        "name": "lxml_verify_mutation_roundtrip",
        "summary": "将 appendChild 修改后的文档序列化并交由 lxml.etree 交叉核对子节点结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_core_dom_node_properties(),
        case_core_dom_node_methods(),
        case_chained_title_text_lookup(),
        case_lxml_verify_mutation_roundtrip(),
    ]
    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-methods-lab/explore-methods.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-methods.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:XML DOM

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a2752.zip

下载课程关联工程包:XML DOM

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2765.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习