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 ...
随机推荐
- [转]教你修复win7中复制粘贴失效的问题
教你修复win7中复制粘贴失效的问题 发布时间:2018-01-17 使用win7系统的时候,我们经常需要对立面的内容进行复制粘贴来引用一些网站的内容,不过最近有网友在使用这个 ...
- MySql 5.7安装(随机密码,修改默认密码)两个坑
MySql 5.7安装(随机密码,修改默认密 下载了MySql 最新版本,安装的过程中,发现了很多新特性 1.data目录不见了 在进行my-default.ini配置的时候 (需要配置 # base ...
- 几种梯度下降方法对比(Batch gradient descent、Mini-batch gradient descent 和 stochastic gradient descent)
https://blog.csdn.net/u012328159/article/details/80252012 我们在训练神经网络模型时,最常用的就是梯度下降,这篇博客主要介绍下几种梯度下降的变种 ...
- Hadoop:安装ftp over hdfs
https://blog.csdn.net/sptoor/article/details/11484855 https://blog.csdn.net/tengxing007/article/deta ...
- SVN迁移部署
迁移svn并修改目录结构的方法 http://blog.csdn.net/jianxin1009/article/details/8220461 测试成功的命令: svnadmin –parent-d ...
- [Node.js] process.nextTick for converting sync to async
For example we have a function to check the filesize: const fs = require('fs'); function fileSize (f ...
- javascript获取时间差
function GetDateDiff(startTime, endTime, diffType) { //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 startTime ...
- JavaScript 时间、格式、转换及Date对象总结
悲剧的遇到问题,从前台得到时间,“Tue Jan 29 16:13:11 UTC+0800 2008”这种格式的,想再后台解析成想要的格式,但是在后台就是解析不了SimpleDateFormat也试着 ...
- javascript中this的妙用
this是javascript语言的一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. this总是指向对象,并且为调用函数的那个对象: //调用普通函数 function f ...
- 牛客网-《剑指offer》-替换空格
题目:http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423 C++ class Solution { public: vo ...