环境

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. 创建 window service 定时任务

    参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...

  2. windows下eclipse+hadoop2

    windows下eclipse+hadoop2.4开发手册 1.解压下载的hadoop2.4,到任意盘符,例如D:\hadoop-2.4.0. 2.设置环境变量 ①新建系统变量,如下所示. ②将新建的 ...

  3. PageMapAdapter MapAdapter (续webServices)

    public class PageMapAdapter extends XmlAdapter<PageMapConverter, IPage<Map<String, Object&g ...

  4. WebSocket基于javaweb+tomcat的简易demo程序

    由于项目需要,前端向后台发起请求后,后台需要分成多个步骤进行相关操作,而且不能确定各步骤完成所需要的时间 倘若使用ajax重复访问后台以获取实时数据,显然不合适,无论是对客户端,还是服务端的资源很是浪 ...

  5. java_设计模式_模板方法模式_Template Method Pattern(2016-08-11)

    定义: 定义一个操作中算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤.这里的算法的结构,可以理解为你根据需求设计出来的业务流程.特定的步骤就是指那些 ...

  6. windowsphone 中CollectionViewSource和ObservableCollection的使用

    功能描述:一级菜单省份  联动显示省份下的城市 直接上代码 public class City { public string Num { get; set; } public string Name ...

  7. JavaScript 获取Select标签选中的项

    <select name="select1" id="select1" onchange=setInput()> <option value= ...

  8. 理解Python的*args, **kwargs

    1 # coding=utf-8 2 def speak(*args, **kwargs): 3 print args, kwargs 4 5 6 a = 1 7 b = 2 8 c = 3 9 sp ...

  9. Bow模型(解释的很好)

    Bag-of-words model (BoW model) 最早出现在NLP和IR领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达一段文字或一个文档. 近年来, BoW模 ...

  10. 转:GraphicsMagick介绍及安装

    原文来自于:http://www.cnblogs.com/cocowool/archive/2010/08/16/1800954.html GraphicsMagick 当前稳定版本:1.3.12(发 ...