#conding:utf-8
# -*- coding:utf-8 -*-
__author__ = 'hdfs'
"""
XML 解析 :DOM解析珍整个文档作为一个可遍历的对象 提交给应用程序,dom解析会将文档全部load进内存,这样对于大型的xml可能性能不多好。
""" import pprint
import xml.dom.minidom
from xml.dom.minidom import Node doc=xml.dom.minidom.parse('books.xml') mapping={}
#遍历DOM对象
for node in doc.getElementsByTagName("book"):
#获取属性
isbn=node.getAttribute('isbn')
#子节点
L=node.getElementsByTagName('title')
for node2 in L:
title=""
for node3 in node2.childNodes:
#Node.Text_NODE:文字节点,
if node3.nodeType == Node.TEXT_NODE:
title+=node3.data
mapping[isbn]=title
pprint.pprint(mapping)

books.xml:

<catalog>
<book isbn="0-596-00128-2">
<title>Python &amp; XML</title>
<title>Python &amp; HTML</title>
<date>December 2001</date>
<author>Jones, Drake</author>
</book>
<book isbn="0-596-15810-6">
<title>Programming Python, 4th Edition</title>
<date>October 2010</date>
<author>Lutz</author>
</book>
<book isbn="0-596-15806-8">
<title>Learning Python, 4th Edition</title>
<date>September 2009</date>
<author>Lutz</author>
</book>
<book isbn="0-596-15808-4">
<title>Python Pocket Reference, 4th Edition</title>
<date>October 2009</date>
<author>Lutz</author>
</book>
<book isbn="0-596-00797-3">
<title>Python Cookbook, 2nd Edition</title>
<date>March 2005</date>
<author>Martelli, Ravenscroft, Ascher</author>
</book>
<book isbn="0-596-10046-9">
<title>Python in a Nutshell, 2nd Edition</title>
<date>July 2006</date>
<author>Martelli</author>
</book>
<!-- plus many more Python books that should appear here -->
</catalog>

最新文章

  1. iOS持续写文件到本地
  2. jquery.validate.js的remote用法
  3. java中IO流操作的标准异常类
  4. MYSQL数据库性能调优之三:explain分析慢查询
  5. 自己定义标签中tagsupport的一些方法
  6. 可视化面板LogDashboard使用log4net源
  7. ivew Tooltip
  8. Oracle不能连接故障排除[TNS-12541: TNS: 无监听程序]
  9. ES7的新特性
  10. ajax 上传文件,监听进度(progress)
  11. BZOJ3524[Poi2014]Couriers——主席树
  12. WCF客户端从服务器下载数据
  13. L1-048. 矩阵A乘以B
  14. shell脚本实例-内存磁盘使用警告
  15. const引用返回值
  16. [MAC OS] NSButton tag 获取
  17. 手把手教你搭建hadoop+hive测试环境(新手向)
  18. python与c语言交互应用实例
  19. keras系列︱利用fit_generator最小化显存占用比率/数据Batch化
  20. elasticsearch5.6.8中文分词器

热门文章

  1. 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道
  2. URL重写IIS7(URL Rewrite Module) 比之前的urlrewrite更方便使用
  3. maven中的继承和聚合的关系
  4. [LeetCode] Find Minimum in Rotated Sorted Array 二分搜索
  5. 【jetty】jetty服务器的使用
  6. loj 6278 6279 数列分块入门 2 3
  7. Ubuntu14.04 LTS安装docker
  8. LeetCode OJ-- Remove Nth Node From End of List
  9. AC日记——营业额统计 1296 codevs
  10. centos7.2安装tomcat8