网站升级正在升级维护,建网站要先建什么,罗湖做网站,上海电商网站建设公司排名python中可以使用xml.dom.minidom模块读取解析xml文件
xml.dom.minidom模块应该是内置模块不用下载安装 对于一个xml文件来说比如这个xml文件的内容为如下
excel version1.0 authorhuangzhihuitable id1colum idexcel version1.0 authorhuangzhihuitable id1colum id1.1 nameMike1 width1 height1 /colum id1.2 nameJohn1 width2 height2 /colum id1.3 nameLucy1 width3 height3 //tabletable id2colum id2.1 nameMike1 width1 height1 /colum id2.2 nameJohn1 width2 height2 /colum id2.3 nameLucy1 width3 height3 //table
/excel代码如下 from xml.dom import minidomdoc minidom.parse(rC:\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()代码打印结果展示