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

下面是我做的demo

运行效果:

解析的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()

最新文章

  1. JavaWeb——ServletContext
  2. python 内置函数!
  3. 如何在Python中实现这五类强大的概率分布
  4. 洛谷P1710 地铁涨价
  5. 实记JLink刷固件方法
  6. 探索 Linux 内存模型--转
  7. Introduction to object
  8. 关于phonegap
  9. UIVIew之霓虹灯实现
  10. Linux修改SSH连接数 重启SSH服务
  11. 第15章-输入/输出 --- 理解Java的IO流
  12. jsp内置对象-config对象
  13. 转摘app-稳定性测试
  14. 【Spring Boot】使用JDBC 获取相关的数据
  15. python 内置函数(二) 进阶函数 递归内容及二分法查找 知识点
  16. 吴恩达-coursera-机器学习-week4
  17. 关于chrome的开发调试方式
  18. Linux操作系统入门学习总结(2015.10)
  19. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 规范与部署
  20. SPOJ375 Query on a tree 【倍增,在线】

热门文章

  1. RESTful Web 服务:教程
  2. Operfire 安装指南
  3. webpack轻松入门教程
  4. JS实现幸运抽奖页面
  5. web项目引入extjs小例子
  6. Effective STL 学习笔记 31:排序算法
  7. [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接
  8. Rookey.Frame v1.0 视频教程之三发布-框架核心思想介绍
  9. 使用spring-boot-maven-plugin插件打包spring boot项目
  10. bzoj 1211: [HNOI2004]树的计数