目錄
第一部分:高級節點操作與遍歷方法
1.1 更精確的節點導航
1.2 使用 cloneNode() 復制節點
1.3 節點插入、替換與高級管理
第二部分:文檔創建與高級輸出控制
2.1 使用 Document 工廠方法完整創建文檔
2.2 高級輸出與序列化控制
第三部分:實用工具函數與模式處理
3.1 處理空白文本節點的實用函數集
3.2 處理文檔類型定義(DTD)
第四部分:模擬ARXML操作
第五部分:性能優化與最佳實踐
5.1 內存管理
5.2 選擇性解析
總結
訪問上一篇:??????Python與XML文件處理詳解(2):使用xml.dom.minidom模塊處理XML
第一部分:高級節點操作與遍歷方法
除了基礎的 getElementsByTagName
,minidom
提供了更多定位和操作節點的工具。
1.1 更精確的節點導航
在處理由工具生成的XML(如ARXML)時,其結構往往是可預測的。利用這一點,我們可以進行精確導航。
import xml.dom.minidom as minidomdom = minidom.parse('books.xml')
root = dom.documentElement# 假設我們知道第一個book元素下第一個元素是author
first_book = root.getElementsByTagName('book')[0]
first_child_of_book = first_book.firstChild# 但firstChild可能是文本節點(換行符),我們需要找到第一個元素節點
first_element_child = first_book.firstChild
while first_element_child is not None and first_element_child.nodeType != first_element_child.ELEMENT_NODE:first_element_child = first_element_child.nextSiblingprint(f"第一個元素子節點的標簽是: {first_element_child.tagName}") # 輸出: author# 編寫一個通用函數來獲取第一個元素子節點
def get_first_element_child(node):child = node.firstChildwhile child is not None:if child.nodeType == child.ELEMENT_NODE:return childchild = child.nextSiblingreturn Noneauthor_node = get_first_element_child(first_book)
1.2 使用 cloneNode()
復制節點
這在需要復制一個復雜節點結構時非常有用,例如在ARXML中復制一個ECU配置模板。
# 深度復制第一個book元素及其所有子孫節點
book_clone = first_book.cloneNode(deep=True)# 修改克隆體的ID,避免沖突
book_clone.setAttribute('id', 'bk103_clone')# 將克隆體添加到文檔中
root.appendChild(book_clone)# 淺復制(deep=False)只復制元素本身,不復制其子節點。適用于創建空模板。
empty_book_template = first_book.cloneNode(deep=False)
empty_book_template.setAttribute('id', 'new_template')
# 此時 empty_book_template 沒有 author, title 等子元素
1.3 節點插入、替換與高級管理
# 創建一個新的price元素
new_price = dom.createElement('price')
new_price.appendChild(dom.createTextNode('75.00'))# --- 在特定位置插入 ---
# 在author節點之后插入new_price
# 首先需要找到參考節點(author)和父節點
ref_node = first_book.getElementsByTagName('author')[0]
first_book.insertBefore(new_price, ref_node.nextSibling) # 插入到author之后# --- 替換節點 ---
old_price = first_book.getElementsByTagName('price')[1] # 假設有第二個price
first_book.replaceChild(new_price, old_price) # 用new_price替換old_price# --- 檢查節點關系 ---
# 判斷一個節點是否包含另一個節點
contains = first_book.hasChildNodes() # True
is_contained = new_price.parentNode.isSameNode(first_book) # True# 獲取同級節點中的下一個元素節點(跳過文本節點)
def get_next_element_sibling(node):sibling = node.nextSiblingwhile sibling is not None:if sibling.nodeType == sibling.ELEMENT_NODE:return siblingsibling = sibling.nextSiblingreturn Nonenext_ele = get_next_element_sibling(ref_node) # 獲取author后面的元素,可能是title
</