python怎么操作xml文件詳細介紹鏈接:https://www.jb51.net/article/50812.htm
從結構上來說,xml很像常見的HTML超文本標記語言。不過超文本語言被設計用來顯示數據,其焦點是數據的外觀。xml被設計用來傳輸和存儲數據,其焦點是數據的內容。
特征:
1. 標簽對組成:<TEST></TEST>
2. 標簽可以有屬性<TEST Loop="1"></TEST>
3. 標簽可以嵌入數據:<TEST>CPU</TEST>
4. 標簽可以嵌入子標簽(具有層級關系)
Python讀取xml
import xml.dom.minidom
打開xml文件:xml.dom.minidom.parse()
每個節點都有nodeName, nodeValue, nodeType,nodeName為節點名字,nodeValue是節點的值,只對文本節點有效。catalog是ELEMENT_NODE類型
現在有以下幾種:
'ATTRIBUTE_NODE'
'CDATA_SECTION_NODE'
'COMMENT_NODE'
'DOCUMENT_FRAGMENT_NODE'
'DOCUMENT_NODE'
'DOCUMENT_TYPE_NODE'
'ELEMENT_NODE'
'ENTITY_NODE'
'ENTITY_REFERENCE_NODE'
'NOTATION_NODE'
'PROCESSING_INSTRUCTION_NODE'
'TEXT_NODE'
舉個例子,有這樣一份xml:
abc.xml

<?xml version="1.0" encoding="utf-8"?> <catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item> </catalog>
讀取根節點:

from xml.dom.minidom import parse def read_xml_root_node(xml_path): dom = parse(xml_path) root = dom.documentElement return root if __name__ == "__main__": root_node = read_xml_root_node("abc.xml") print(root_node.nodeName) print(root_node.nodeType)
輸出結果:
catalog
1
為什么打印出來的類型是1呢,1代表什么呢。參考nodeType。
獲取子節點以及value:

from xml.dom.minidom import parse def read_xml_root_node(xml_path): dom = parse(xml_path) root = dom.documentElement return root def read_child_label(node, label_name): child = node.getElementsByTagName(label_name) return child if __name__ == "__main__": root_node = read_xml_root_node("abc.xml") print(root_node.nodeName) print(root_node.nodeType) child_nodes = read_child_label(root_node, "maxid") for child_node in child_nodes: print(child_node.nodeName) print(child_node.nodeType) print(child_node.childNodes[0].nodeValue)
輸出結果:
catalog 1 maxid 1 4
獲取標簽屬性

from xml.dom.minidom import parse def read_xml_root_node(xml_path): dom = parse(xml_path) root = dom.documentElement return root def read_child_label(node, label_name): child = node.getElementsByTagName(label_name) return child def read_attribute(node, attr_name): attribute = node.getAttribute(attr_name) return attribute if __name__ == "__main__": root_node = read_xml_root_node("abc.xml") print(root_node.nodeName) print(root_node.nodeType) child_nodes_login = read_child_label(root_node, "login") for child_node in child_nodes_login: attr_username = read_attribute(child_node, "username") print(attr_username)
輸出結果:
catalog 1 pytest
另一種模塊讀取xml的方法,可以遍歷指定標簽下的子標簽

from xml.etree import ElementTree as ET per = ET.parse("abc.xml") p = per.findall("./login/item") for opener in p: for child in opener.getchildren(): print(child.tag, ":", child.text) p = per.findall("./item") for oneper in p: for child in oneper.getchildren(): print(child.tag, ":", child.text)
輸出結果:
caption : 測試
caption : Zope