python DOM解析XML
2024-09-06 01:14:24
#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 & XML</title>
<title>Python & 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>
最新文章
- iOS持续写文件到本地
- jquery.validate.js的remote用法
- java中IO流操作的标准异常类
- MYSQL数据库性能调优之三:explain分析慢查询
- 自己定义标签中tagsupport的一些方法
- 可视化面板LogDashboard使用log4net源
- ivew Tooltip
- Oracle不能连接故障排除[TNS-12541: TNS: 无监听程序]
- ES7的新特性
- ajax 上传文件,监听进度(progress)
- BZOJ3524[Poi2014]Couriers——主席树
- WCF客户端从服务器下载数据
- L1-048. 矩阵A乘以B
- shell脚本实例-内存磁盘使用警告
- const引用返回值
- [MAC OS] NSButton tag 获取
- 手把手教你搭建hadoop+hive测试环境(新手向)
- python与c语言交互应用实例
- keras系列︱利用fit_generator最小化显存占用比率/数据Batch化
- elasticsearch5.6.8中文分词器
热门文章
- 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道
- URL重写IIS7(URL Rewrite Module) 比之前的urlrewrite更方便使用
- maven中的继承和聚合的关系
- [LeetCode] Find Minimum in Rotated Sorted Array 二分搜索
- 【jetty】jetty服务器的使用
- loj 6278 6279 数列分块入门 2 3
- Ubuntu14.04 LTS安装docker
- LeetCode OJ-- Remove Nth Node From End of List
- AC日记——营业额统计 1296 codevs
- centos7.2安装tomcat8