# -*- 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. Error: Program type already present: android.arch.lifecycle.LifecycleRegistry$1

    com.firebaseui:firebase-ui-firestore:3.1.0 depends on android.arch.lifecycle:extensions:1.0.0-beta1. ...

  2. ubuntu 终端无法启动:ImportError: cannot import name 'sysconfig' from 'distutils'

    gnome-terminal 出错 ImportError: cannot import name '_gi' 系统:ubuntu17 装了python2.7.13, 之后陆续装了python3.5. ...

  3. 【转】group_concat函数详解

    转自: http://hchmsguo.iteye.com/blog/555543 问了好多人,都不知道group_concat这个函数. 这个函数好啊,能将相同的行组合起来,省老事了. MySQL中 ...

  4. 使用idea 在springboot添加本地jar包的方法

    原文地址;https://blog.csdn.net/huxiaodong1994/article/details/80702278 1.首先在与src同级的目录下新建一个lib目录,然后将本地jar ...

  5. Swift: Swift中Selector的变化

    Swift中Selector变化 2.2 之前,使用字符串作为方法名称 //无参数 btn.addTarget(self, action: Selector("buttonPress&quo ...

  6. 在ASP.NET MVC中以post方式传递数组参数的示例【转】

    最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象 ...

  7. 混合开发 Hybird Cordova PhoneGap web 跨平台 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. PowerDesigner设置表字段唯一约束

    现有一个表student,id为主键. 现打算给name添加一个唯一约束. 双击打开这个表,点击“keys"选项卡,显示有一个key_1,这是id主键. 点击”insert a row'插入 ...

  9. LDA(latent dirichlet allocation)的应用

    http://www.52ml.net/1917.html 主题模型LDA(latent dirichlet allocation)的应用还是很广泛的,之前我自己在检索.图像分类.文本分类.用户评论的 ...

  10. spring 读取配置文件,将值注入到静态字段

    resources/config/config-dev.properties es.ip.node=xxxxxxxcluster.name=xxxxxxxclient.transport.sniff= ...