1、xml 文件如下
// data_info.xml xml version="1.0" encoding="utf-8"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/"> <string name = "province01">直辖市string> <string name = "province02">特别行政区string> <string name = "province03">黑龙江string> <string name = "province04">吉林string> <string name = "province05">辽宁string> <string name = "province06">内蒙古string> <string name = "province07">河北string> <string name = "province08">河南string> <string name = "province09">山东string> <string name = "province10">山西string> <string name = "province11">江苏string> <string name = "province12">安徽string> <string name = "province13">陕西string> ArrayOfString>
2、通过 minidom 模块读取 xml 文件内容
from xml.dom import minidom # parse() 获取 DOM 对象 dom = minidom.parse("data_info.xml") # 获取根节点 root = dom.documentElement # 通过 dom 对象或根元素,再根据标签名获取元素节点,是个列表 tag = root.getElementsByTagName("string") # 读取标签对之间的数据,标签从上到下按照下标从 0 开始 print(tag[1].firstChild.data) # 获取节点属性 print(tag[0].getAttribute("name")) # 结果如下 特别行政区 province01