# -*- 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的更多相关文章

  1. Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数

    总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段. from xml.dom.minidom im ...

  2. python 应用xml.dom.minidom读xml

    xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...

  3. ElementTree 解析xml(minidom解析xml大文件时,MemoryError)

    在使用minido解析xml文件时,因为文件过大,结果报错MemoryError.查询后得知是因为minidom在解析时是将所有文件放到内存里的,很占用内存,所以要考虑换一种方法来处理xml文件.   ...

  4. xml dom minidom

    一. xml相关术语: 1.Document(文档): 对应一个xml文件 2.Declaration(声明): <?xml version="1.0" encoding=& ...

  5. 创建xml文件、解析xml文件

        1.创建XML文件: import codecs import xml.dom.minidom doc=xml.dom.minidom.Document() print doc root=do ...

  6. python XML文件解析:用xml.dom.minidom来解析xml文件

    python解析XML常见的有三种方法: 一是xml.dom.*模块,是W3C DOM API的实现,若需要处理DOM API则该模块很合适, 二是xml.sax.*模块,它是SAX API的实现,这 ...

  7. python模块:xml.dom.minidom

    """Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...

  8. Python:Sqlmap源码精读之解析xml

    XML <?xml version="1.0" encoding="UTF-8"?> <root> <!-- MySQL --&g ...

  9. 用JAXP的dom方式解析XML文件

    用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...

随机推荐

  1. SVG渲染顺序及z轴显示问题(zIndex)

    SVG是严格按照定义元素的顺序来渲染的,这个与HTML靠z-index值来控制分层不一样. 在SVG中,写在前面的元素先被渲染,写在后面的元素后被渲染.后渲染的元素会覆盖前面的元素,虽然有时候受透明度 ...

  2. Redis 性能问题的记录

    最近线上使用redis, 查询的情况不甚理想, 这个查询操作是个 lua 脚本, 包含如下操作 开发机 redis, 没有其他干扰, 插入的 zset 有 5000 member 左右, 使用的 re ...

  3. 解决MTP device安装失败,手机无法被读取

    操作步骤: 1.  我的电脑—>管理—>设备管理器—>便携设备(MTP)-->右击-更新驱动 2.  选择浏览计算机以查找驱动程序软件—>从计算机列表选择—>MTP ...

  4. 第三十一章 elk(2)- 第二种架构(最常用架构)

    参考:http://linuxg.blog.51cto.com/4410110/1761757 最常用架构: 一.安装redis 1.下载:http://redis.io/download 2.解压后 ...

  5. leetcode Roman Integer

    class Solution { public: int romanToInt(string s) { if (s.length() < 1) return 0; map<char,int ...

  6. json传输二进制的方案【转】

    本文转自:http://wiyi.org/binary-to-string.html json 是一种很简洁的协议,但可惜的是,它只能传递基本的数型(int,long,string等),但不能传递by ...

  7. em px pt单位介绍及换算

    PX\EM\PT单位介绍 px Pixel单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言的国内推荐:em单位名称为相对长度单位.相对于当前对象内文本的字体尺寸,国外使用比较多, ...

  8. 谷歌(Google)被墙,解决地图和字体无法显示的问题

    首先,本文以及本站所有文章都是技术探讨文章,不鼓励任何人去fan qiang以及做任何违法的事情.接下来是正文: 谷歌基本上是被和谐透了,谷歌地图API自然也打不开了,于是公司网站上那些谷歌地图都变成 ...

  9. python3 杀死进程

    在windows10杀死进程:Shadowsocks.exe os.system('taskkill /f /im %s' % 'Shadowsocks.exe')

  10. 最全的spark基础知识解答

    原文:http://www.36dsj.com/archives/61155 一. Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduc ...