python中如何使用xml.dom.minidom模块读取解析xml文件
更新时间:2023年10月17日 16:02:00 作者:1夜的终章1
xml.dom.minidom模块应该是内置模块不用下载安装,本文给大家介绍python中如何使用xml.dom.minidom模块读取解析xml文件,感兴趣的朋友一起看看吧
python中可以使用xml.dom.minidom模块读取解析xml文件
xml.dom.minidom模块应该是内置模块不用下载安装
对于一个xml文件来说比如这个xml文件的内容为如下
<excel version="1.0" author="huangzhihui">
<table id="1">
<colum id="1.1" name="Mike1" width="1" height="1" />
<colum id="1.2" name="John1" width="2" height="2" />
<colum id="1.3" name="Lucy1" width="3" height="3" />
</table>
<table id="2">
<colum id="2.1" name="Mike1" width="1" height="1" />
<colum id="2.2" name="John1" width="2" height="2" />
<colum id="2.3" name="Lucy1" width="3" height="3" />
</table>
</excel>代码如下
from xml.dom import minidom
doc = minidom.parse(r'C:\Users\xxxxxxx\Desktop\test.xml') #解析xml文件(句柄或文件路径)
#doc = minidom.parseString() #解析xml字符串
root_node = doc.documentElement #获得根节点对象
xml_excel_obj_list = root_node.getElementsByTagName('excel')
print(xml_excel_obj_list)
xml_table_obj_list = root_node.getElementsByTagName('table')
print(xml_table_obj_list)
for table in xml_table_obj_list:
print("==========================")
lines_obj_list = table.getElementsByTagName('colum')
for line_obj in lines_obj_list:
print(line_obj.getAttribute("name"), line_obj.getAttribute("width"), line_obj.getAttribute("height"))
print("==========================")代码打印结果展示

到此这篇关于python中使用xml.dom.minidom模块读取解析xml文件的文章就介绍到这了,更多相关python读取解析xml文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
python pytest进阶之xunit fixture详解
这篇文章主要介绍了python pytest进阶之xunit fixture详解,了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearDown方法,那么在pytest框架中同样存在类似的方法,今天我们就来具体说明,需要的朋友可以参考下2019-06-06
python正则表达式函数match()和search()的区别
match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢?本文详细介绍了这2个函数的区别2021-10-10


最新评论