环境

python:3.4.4

准备xml文件

首先新建一个xml文件,countries.xml。内容是在python官网上看到的。

<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>

准备python文件

新建一个test_DOM.py,用来解析xml文件。

#!/usr/bin/python
# -*- coding: UTF-8 -*- from xml.dom.minidom import parse
import xml.dom.minidom DOMTree = xml.dom.minidom.parse("countries.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("data"):
print ("Root element : %s" % collection.getAttribute("data")) countries = collection.getElementsByTagName("country") for country in countries:
print ("*****Country*****")
if country.hasAttribute("name"):
print ("Name: %s" % country.getAttribute("name"))
rank = country.getElementsByTagName('rank')[0]
print ("Rank: %s" % rank.childNodes[0].data)
year = country.getElementsByTagName('year')[0]
print ("Year: %s" % year.childNodes[0].data)
gdppc = country.getElementsByTagName('gdppc')[0]
print ("Gdppc: %s" % gdppc.childNodes[0].data)
neighbors = country.getElementsByTagName('neighbor')
for neighbor in neighbors:
print ("Neighbor:", neighbor.getAttribute("name"),neighbor.getAttribute("direction"))

执行结果

>python test_DOM.py
*****Country*****
Name: Liechtenstein
Rank: 1
Year: 2008
Gdppc: 141100
Neighbor: Austria E
Neighbor: Switzerland W
*****Country*****
Name: Singapore
Rank: 4
Year: 2011
Gdppc: 59900
Neighbor: Malaysia N
*****Country*****
Name: Panama
Rank: 68
Year: 2011
Gdppc: 13600
Neighbor: Costa Rica W
Neighbor: Colombia E

备注

DOM(Document Object Model)

DOM是一个W3C的跨语言的API,用来读取和更改 XML 文档。

一个DOM解析器在解析一个XML文档时,一次性读取整个文档,把文档中的所有元素保存在内存中的一个树结构中,之后可以对这个树结构进行读取或修改,也可以把修改过的树结构写入xml文件。

参见: https://docs.python.org/2/library/xml.dom.html

DOMTree = xml.dom.minidom.parse("countries.xml")

使用 xml.dom.minidom解析器打开 countries.xml 文件,并返回一个 Document对象,也就是树结构。Document 对象代表了整个 XML 文档,包括它的元素、属性、处理指令、备注等。

参见: https://docs.python.org/2/library/xml.dom.minidom.html

Return a Document from the given input. filename_or_file may be either a file name, or a file-like object. parser, if given, must be a SAX2 parser object. This function will change the document handler of the parser and activate namespace support; other parser configuration (like setting an entity resolver) must have been done in advance.

collection = DOMTree.documentElement

返回 DOMTree的根元素。

Document.documentElement
The one and only root element of the document.

rank = country.getElementsByTagName('rank')[0]

从country往下寻找所有 tag名为“rank”的元素节点,将找到的第一个节点赋值给 rank。

Document.getElementsByTagName(tagName)
Search for all descendants (direct children, children’s children, etc.) with a particular element type name.

collection.getAttribute("data")

获取并返回 collection 的“data”属性值。如果collection没有“data”属性,则返回一个空的字符串。

Element.getAttribute(name)
Return the value of the attribute named by name as a string. If no such attribute exists, an empty string is returned, as if the attribute had no value.

python 解析xml 文件: DOM 方式的更多相关文章

  1. python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  2. [转载] python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  3. android解析xml文件的方式

    android解析xml文件的方式   作者:东子哥 ,发布于2012-11-26,来源:博客园   在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xm ...

  4. JAVA解析XML之DOM方式

    JAVA解析XML之DOM方式 准备工作 创建DocumentBuilderFactory对象;    创建DocumentBuilder对象; 通过DocumentBuilder对象的parse方法 ...

  5. Java解析XML文件的方式

    在项目里,我们往往会把一些配置信息放到xml文件里,或者各部门间会通过xml文件来交换业务数据,所以有时候我们会遇到“解析xml文件”的需求.一般来讲,有基于DOM树和SAX的两种解析xml文件的方式 ...

  6. python 解析xml 文件: Element Tree 方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  7. PYTHON解析XML的多种方式效率对比实测

    在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜索后发现,目前应用比较广泛,且效率相对较高的E ...

  8. 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  9. 【TensorFlow】Python解析xml文件

    最近在项目中使用TensorFlow训练目标检测模型,在制作自己的数据集时使用了labelimg软件对图片进行标注,产生了VOC格式的数据,但标注生成的xml文件标签值难免会产生个别错误造成程序无法跑 ...

随机推荐

  1. sql数值显示成千分位分隔符的形式

    ), )--带小数点 ), ),'.00','')--不带小数点

  2. 输出内容(document.write)

    document.write() 直接在页面中输出内容 第一种 直接输出 document.write("I Love Javascript !") //输出内容为:I Love ...

  3. Linux通配符

    * 任意字符 ?任意单个字符 [] 匹配指定 字符范围内的字符 [^] 指定范围之外的单个字符 常规字符集合 [a-z] a到z的所有小写字母 [A-Z] a到z的所有大写字母 [0-9] 0到9的所 ...

  4. 神秘链接__proto__是什么鬼

    _proto_实际上是某个实例对象的隐藏属性,而prototype是其构造器函数(或者说‘类’)的原型属性; function Mine() {} var  hi = new Function(), ...

  5. 简单概述 .NET Framework 各版本区别

    目前已发行的版本有1.0.1.1.2.0.3.0.3.5.4.0.4.5(及4.5.1.4.5.2).4.6(及4.6.1). 1.0版本:最初的.net framework版本,作为一个独立的工具包 ...

  6. 如何去掉textarea右下角的灰色角标?

    在css中定义: resize: none; ,这个样式同时禁用textarea调整大小

  7. dedecms修改templets为别的名字

    修改templets模板文件夹的方法: 首先找到系统配置文件common.inc.php,此文件存放在Include目录下,打开common.inc.php来修改默认模板目录templets, 查找: ...

  8. Apache:To Config The Vhost of Django Project

    It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...

  9. BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&hibernate

    1.在lib中加入必要的包,导入后结果如下: lib打包下载:SSH-lib.jar  (struts2.3.1.2  spring3.0.5 hibernate3.6.10.Final) 只包含必要 ...

  10. 在linux下实现UBOOT的TFTP下载功能

    一.环境 1.条件 软件:虚拟机下linux(本文涉及到的是Ubuntu12.0.4). linux下的串口助手(例如minicom)或windows下的串口助手(例如超级终端.SecureCRT) ...