python开发_xml.dom_解析XML文档_完整版_博主推荐

时间:2021-06-26 15:13:01

在阅读之前,你需要了解一些xml.dom的一些理论知识,在这里你可以对xml.dom有一定的了解,如果你阅读完之后。

下面是我做的demo

运行效果:

python开发_xml.dom_解析XML文档_完整版_博主推荐

解析的XML文件位置:c:\\test\\hongten.xml

 <?xml version="1.0" encoding="UTF-8"?>
<students>
<student no="2009081097">
<name>Hongten</name>
<gender>M</gender>
<age>20</age>
<score subject="math">97</score>
<score subject="chinese">90</score>
</student>
<student no="2009081098">
<name>DuDu</name>
<gender>W</gender>
<age>21</age>
<score subject="math">87</score>
<score subject="chinese">96</score>
</student>
<student no="2009081099">
<name>Sum</name>
<gender>M</gender>
<age>19</age>
<score subject="math">64</score>
<score subject="chinese">98</score>
</student>
</students>

====================================================

代码部分:

====================================================

 #python xml.dom

 #Author   :   Hongten
#Mailto : hongtenzone@foxmail.com
#Blog : http://www.cnblogs.com/hongten
#QQ : 648719819
#Version : 1.0
#Create : 2013-09-03 import os
from xml.dom import minidom #global var
SHOW_LOG = True
XML_PATH = None def get_dom_by_parse(path):
'''根据XML文件地址解析XML文件,返回dom对象'''
if os.path.exists(path):
if SHOW_LOG:
print('开始解析XML文件:[{}]'.format(path))
return minidom.parse(path)
else:
print('the path [{}] dose not exist!'.format(path)) def get_dom_by_file(path):
'''解析作为文档打开的XML文件'''
if os.path.exists(path):
if SHOW_LOG:
print('开始打开XML文件:[{}]'.format(path))
with open(path) as pf:
if SHOW_LOG:
print('开始解析XML文件:[{}]'.format(path))
return minidom.parse(pf)
else:
print('the path [{}] dose not exist!'.format(path)) def get_dom_by_string(s):
'''解析以字符串形式的XML数据格式'''
if s is not None and s != '':
if SHOW_LOG:
print('开始解析字符串形式的XML数据:[{}]'.format(s))
return minidom.parseString(s)
else:
print('the input string is None or equals \'\'.') def get_root(dom):
'''返回XML文件的根节点'''
if dom is not None:
return dom.documentElement
else:
print('the dom is None!') def get_element_children(fatherElement, subNodeName):
'''根据父节点fatherElement获取子节点subNodeName'''
if fatherElement is not None:
if subNodeName is not None and subNodeName != '':
return fatherElement.getElementsByTagName(subNodeName)
else:
print('the sub node name is None or equals \'\'.')
else:
print('the father node is None!') def get_element_value(element, index=0):
'''获取节点的值'''
if element is not None:
return element.childNodes[index].nodeValue
else:
print('the element is None!') def get_element_attrib_value(element, name):
'''根据节点element的属性名称name获取属性名称的值'''
if element is not None:
if name is not None and name != '':
return element.getAttribute(name)
else:
print('the name is None or equals \'\'.')
else:
print('the element is None!') def get_info(root_children):
'''解析XML内容'''
info = []
for item in root_children:
subs = []
score_value = []
i_no = get_element_attrib_value(item, 'no')
i_name = get_element_children(item, 'name')
i_gender = get_element_children(item, 'gender')
i_age = get_element_children(item, 'age')
i_score = get_element_children(item, 'score')
for sub in i_score:
i_sub = get_element_attrib_value(sub, 'subject')
subs.append(i_sub) v_name = get_element_value(i_name[0])
v_gender = get_element_value(i_gender[0])
v_age = get_element_value(i_age[0])
for s in range(len(i_score)):
score_value.append(s)
v_score = dict(zip(subs, score_value))
info.append(v_name)
info.append(v_gender)
info.append(v_age)
info.append(v_score)
return info def init():
global SHOW_LOG
SHOW_LOG = True
global XML_PATH
XML_PATH = 'C:\\test\\hongten.xml' def main():
init()
dom = get_dom_by_parse(XML_PATH)
root = dom.documentElement
print(root)
root_children = get_element_children(root, 'student')
print(root_children)
info = get_info(root_children)
print(info) if __name__ == '__main__':
main()