python 之模块之 xml.dom.minidom解析xml
# -*- coding: cp936 -*-
#python 27
#xiaodeng
#python 之模块之 xml.dom.minidom解析xml
#http://www.cnblogs.com/coser/archive/2012/01/10/2318298.html
#python有三种方法解析XML,SAX,DOM,以及ElementTree #import xml.dom
#这里主要通过xml.dom.minidom创建xml文档,然后解析用以熟悉api
#常用方法function()
'''
minidom.parse(filename) #加载和读取xml文件
doc.documentElement #获取xml文档对象
node.getAttribute(AttributeName) #获取xml节点属性值
node.getElementsByTagName(TagName) #获取xml节点对象集合
node.childNodes #获取子节点列表
node.childNodes[index].nodeValue #获取xml节点值
node.firstChild #访问第一个节点
n.childNodes[0].data #获得文本值
node.childNodes[index].nodeValue #获取XML节点值 doc=minidom.parse(filename)
doc.toxml('utf-8') #返回Node节点的xml表示的文本
''' #test.xml
'''
<collection shelf="New Arrivals">
<movie title="Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year>1989</year>
<rating>R</rating>
<stars>8</stars>
<description>A schientific fiction</description>
</movie>
<movie title="Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes>4</episodes>
<rating>PG</rating>
<stars>10</stars>
<description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars>2</stars>
<description>Viewable boredom</description>
</movie>
</collection>
''' #解析案例
from xml.dom import minidom
doc=minidom.parse('test.xml') #parse("foo.xml")
#parseString("<foo><bar/></foo>") #实例化
root=doc.documentElement #注意没括号 #文档对象元素
print '--'*25
print root.nodeName #节点名字,collection
print root.nodeValue #节点的值,None
print root.nodeType #节点类型,1
print root.ELEMENT_NODE #
print '--'*25 #在集合中获取所有电影
nodes=root.getElementsByTagName('movie') #获取xml节点对象集合 #打印每部电影的详细信息
for n in nodes:
#print n#<DOM Element: movie at 0x1f9d968> #获得电影的title的属性值
#print n.getAttribute('title') #获取xml节点type对象的具体信息
type= n.getElementsByTagName('type')[0]
print "Type:%s" % type.childNodes[0].data##获得文本值
python 之模块之 xml.dom.minidom解析xml的更多相关文章
- Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数
总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段. from xml.dom.minidom im ...
- python 应用xml.dom.minidom读xml
xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...
- ElementTree 解析xml(minidom解析xml大文件时,MemoryError)
在使用minido解析xml文件时,因为文件过大,结果报错MemoryError.查询后得知是因为minidom在解析时是将所有文件放到内存里的,很占用内存,所以要考虑换一种方法来处理xml文件. ...
- xml dom minidom
一. xml相关术语: 1.Document(文档): 对应一个xml文件 2.Declaration(声明): <?xml version="1.0" encoding=& ...
- 创建xml文件、解析xml文件
1.创建XML文件: import codecs import xml.dom.minidom doc=xml.dom.minidom.Document() print doc root=do ...
- python XML文件解析:用xml.dom.minidom来解析xml文件
python解析XML常见的有三种方法: 一是xml.dom.*模块,是W3C DOM API的实现,若需要处理DOM API则该模块很合适, 二是xml.sax.*模块,它是SAX API的实现,这 ...
- python模块:xml.dom.minidom
"""Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...
- Python:Sqlmap源码精读之解析xml
XML <?xml version="1.0" encoding="UTF-8"?> <root> <!-- MySQL --&g ...
- 用JAXP的dom方式解析XML文件
用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...
随机推荐
- 【转】ubuntu apt-get update 失败解决
FROM : http://blog.csdn.net/ronghua_liu/article/details/8609450 当运行apt-get update后出现如下错误时:E: Some in ...
- [转]Sphinx+Mysql+中文分词安装-实现中文全文搜索
From : http://blog.csdn.net/lgm252008/article/details/5373436 1.什么是SphinxSphinx 是一个在GPLv2 下发布的一个全文检索 ...
- 【转】Mysql行转换为列
From : http://www.cnblogs.com/lhj588/archive/2012/06/15/2550392.html# 今晚需要统计数据生成简易报表,由原表格数据是单行的形式,最好 ...
- 样条之拉格朗日Lagrange(一元全区间)插值函数
这是使用拉格朗日插值函数生成的样条曲线.在数值分析中,拉格朗日插值法是以法国十八世纪数学家约瑟夫·拉格朗日命名的一种多项式插值方法.许多实际问题中都用函数来表示某种内在联系或规律,而不少函数都只能通过 ...
- boost并发编程boost::atomic
三个用于并发编程的组件: atomic,thread,asio(用于同步和异步io操作) atomic atomic,封装了不同计算机硬件的底层操作原语,提供了跨平台的原子操作功能,解决并发竞争读 ...
- 第十一章 dubbo通信框架-netty4
netty4是2.5.6引入的,2.5.6之前的netty用的是netty3.在dubbo源码中相较于netty3,添加netty4主要仅仅改了两个类:NettyServer,NettyClient. ...
- Cantor展开式
X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0! 其中,a为整数,并且0<=ai<i(1<=i<=n).这就是康托展开 ...
- GridControl常见用法【转】
刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考: 说明: gcTest GridControl gvText GridView //隐藏最上面的G ...
- knockout示例
最近项目需要用到knockout js,有关knockout的介绍网上已经很多很多了,但是很少有比较全面的示例,于是乎我就自己做了一个小demo,已备以后查阅.knockout经常和knockout. ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...