环境

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_ET.py,用来解析xml文件。

#!/usr/bin/python
# -*- coding=utf-8 -*- import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element tree = ET.parse('countries.xml') nodes = tree.findall("country") for node in nodes:
#search node & attribute & text
print ("*****Country*****")
if node.attrib["name"]:
print ("Name:",node.attrib["name"]) rank=node.find("rank")
print ("Rank:",rank.text) year=node.find("year")
print ("Year:",year.text) gdppc=node.find("gdppc")
print ("Gdppc:",gdppc.text) neighbors=node.findall("neighbor")
for neighbor in neighbors:
print ("Neighbor:",neighbor.attrib["name"]) #add node
rank=node.find("rank")
element=Element("rank_next", {"name":"Rank","create":""})
element.text=""
rank.append(element) #delete node
year=node.find("year")
node.remove(year) #add node attribute
node.set("force","NewForce")
#update node attribute
node.set("name","NewNode")
#delete node attribute
neighbors=node.findall("neighbor")
for neighbor in neighbors:
del neighbor.attrib["direction"] #add node text
neighbors=node.findall("neighbor")
for neighbor in neighbors:
neighbor.text = "Hello,Neighbor"
#update node text
gdppc=node.find("gdppc")
gdppc.text = ""
#delete node text
rank=node.find("rank")
rank.text = "" tree.write("./out.xml", encoding="utf-8",xml_declaration=True)

执行结果

控制台:

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

out.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<data>
<country force="NewForce" name="NewNode">
<rank><rank_next create="20151231" name="Rank">5</rank_next></rank>
<gdppc>11111</gdppc>
<neighbor name="Austria">Hello,Neighbor</neighbor>
<neighbor name="Switzerland">Hello,Neighbor</neighbor>
</country>
<country force="NewForce" name="NewNode">
<rank><rank_next create="20151231" name="Rank">5</rank_next></rank>
<gdppc>11111</gdppc>
<neighbor name="Malaysia">Hello,Neighbor</neighbor>
</country>
<country force="NewForce" name="NewNode">
<rank><rank_next create="20151231" name="Rank">5</rank_next></rank>
<gdppc>11111</gdppc>
<neighbor name="Costa Rica">Hello,Neighbor</neighbor>
<neighbor name="Colombia">Hello,Neighbor</neighbor>
</country>
</data>

备注

具有方便友好的API。代码可用性好,速度快,消耗内存少。

最适合用来处理XML文档。

参考:https://docs.python.org/2/library/xml.etree.elementtree.html

tree = ET.parse('countries.xml')

解析countries.xml并返回一个树。

tree.write("./out2.xml", encoding="utf-8",xml_declaration=True)

将元素树写入到文档,采用 “utf-8”编码,具有xml声明。

write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml")
Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding [1] is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). Returns an encoded string.

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

  1. Python 解析 XML 文件生成 HTML

    XML文件result.xml,内容如下: <ccm> <metric> <complexity>1</complexity> <unit> ...

  2. 横向对比分析Python解析XML的四种方式

    横向对比分析Python解析XML的四种方式 在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜 ...

  3. python 解析xml 文件: DOM 方式

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

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

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

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

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

  6. 【TensorFlow】Python解析xml文件

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

  7. python 解析 XML文件

    如下使用xml.etree.ElementTree模块来解析XML文件.ElementTree模块中提供了两个类用来完成这个目的: ElementTree表示整个XML文件(一个树形结构) Eleme ...

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

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

  9. Python解析xml文件遇到的编码解析的问题

    使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...

随机推荐

  1. classpath and path.

    simply talk about the <path> and the <classpath> in java development. when the <path& ...

  2. A除以B_2

    本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...

  3. Objective-C消息机制的原理

    http://desheng.me/2012/03/31/objective-c%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E7%9A%84%E5%8E%9F%E7%90 ...

  4. c#与c++交互的一些东西

    最近做一个项目,对方公司只提供了一个c++的DLL,但没封住,c#无法DllImport.所以只能自己写c++来封住了. 对方的Dll只接收yuv420的图片格式,所以在c++里用opencv来转换. ...

  5. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib 错误的解决办法

    查看项目属性,发现该项目的目标框架是.NET Framework 4 Client Profile ,而被引用的程序集的目标框架是.NET Framework 4,将该项目的目标框架修改成.NET F ...

  6. Oracle数据库之PL/SQL触发器

    Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...

  7. gulp Tips

    npm配置相关属性用于寻找全局安装的module npm install  --save-dev 本地安装   在gulp.src()里指定取用文件的语法是,在[ ]中以字符串形式填写文件名,用&qu ...

  8. PHP面向对象(OOP):__call()处理调用错误

    在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...

  9. bzoj3583: 杰杰的女性朋友 && 4362: Graph

    Description 给出一张n个点的有向图G(V,E).对于任意两个点u,v(u可以等于v),u向v的连边数为: ∑OUT(u,i) * IN(v,i),其中1<=i<=K 其中k和数 ...

  10. MYSQL死锁

    转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/ri0day-logs/59186177.html mysql使用myisam的时候锁表比较多,尤其 ...