python解析xml示例
来源:互联网 发布:4gip网络加速器.apk 编辑:程序博客网 时间:2024/05/16 08:03
python是一种脚本语言,功能非常强大
我们来看看如何使用python来解析xml,举个粟子:
例1:解析xml
<?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>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
for child in root:
print child.tag, child.attrib
结果:
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
作为一个元素来讲,root拥有标签和属性字典:
root.tag 就是'data'
root.attrib 即为{}
遍历其孩子结点,即可以得到更里一层的标签和属性字典
for child in root:
print child.tag, child.attrib
就能得到
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
而child其实也是嵌套的,我们可以以二维数组的方式得到特定的元素:
root[0][1].text 即 2008
此处,你也可以只得到你感兴趣的元素:
例如:
for neighbor in root.iter('neighbor'):
print neighbor.attrib
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}
例2:修改xml
for rank in root.iter('rank'):
new_rank = int(rank.text) + 1
rank.text = str(new_rank)
rank.set('updated', 'yes')
tree.write('output.xml')
修改后的xml如下所示:rank部分被改变了
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
例3,修改xml之2
我们可以通过Element.remove() 接口来删除xml文件中的元素
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
得到结果如下:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
</data>
例4,创建xml
SubElement()接口提供了一种创建子元素的简便的方法
a = ET.Element('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(a, 'c')
d = ET.SubElement(c, 'd')
ET.dump(a)
输出如<a><b /><c><d /></c></a>
构成如下:
<a>
<b />
<c>
<d />
</c>
</a>
例5,以命名空间的方式来解析xml
示例xml:(这个xml文件中添加了两个命名空间,其中一个的前缀是fictional,另外一个作为默认的命名空间)
<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
xmlns="http://people.example.com">
<actor>
<name>John Cleese</name>
<fictional:character>Lancelot</fictional:character>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Eric Idle</name>
<fictional:character>Sir Robin</fictional:character>
<fictional:character>Gunther</fictional:character>
<fictional:character>Commander Clement</fictional:character>
</actor>
</actors>
方式一:在find()或者findall()接口中手动添加URI到每一个标签或者属性值前面。
root = fromstring(xml_text)
for actor in root.findall('{http://people.example.com}actor'):
name = actor.find('{http://people.example.com}name')
print name.text
for char in actor.findall('{http://characters.example.com}character'):
print ' |-->', char.text
方式二:在find()或者findall()接口中创建一个自己的前缀
ns = {'real_person': 'http://people.example.com',
'role': 'http://characters.example.com'}
for actor in root.findall('real_person:actor', ns):
name = actor.find('real_person:name', ns)
print name.text
for char in actor.findall('role:character', ns):
print ' |-->', char.text
两者的输出均为:
John Cleese
|--> Lancelot
|--> Archie Leach
Eric Idle
|--> Sir Robin
|--> Gunther
|--> Commander Clement
我们来看看如何使用python来解析xml,举个粟子:
例1:解析xml
<?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>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
for child in root:
print child.tag, child.attrib
结果:
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
作为一个元素来讲,root拥有标签和属性字典:
root.tag 就是'data'
root.attrib 即为{}
遍历其孩子结点,即可以得到更里一层的标签和属性字典
for child in root:
print child.tag, child.attrib
就能得到
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
而child其实也是嵌套的,我们可以以二维数组的方式得到特定的元素:
root[0][1].text 即 2008
此处,你也可以只得到你感兴趣的元素:
例如:
for neighbor in root.iter('neighbor'):
print neighbor.attrib
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}
例2:修改xml
for rank in root.iter('rank'):
new_rank = int(rank.text) + 1
rank.text = str(new_rank)
rank.set('updated', 'yes')
tree.write('output.xml')
修改后的xml如下所示:rank部分被改变了
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
例3,修改xml之2
我们可以通过Element.remove() 接口来删除xml文件中的元素
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
得到结果如下:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
</data>
例4,创建xml
SubElement()接口提供了一种创建子元素的简便的方法
a = ET.Element('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(a, 'c')
d = ET.SubElement(c, 'd')
ET.dump(a)
输出如<a><b /><c><d /></c></a>
构成如下:
<a>
<b />
<c>
<d />
</c>
</a>
例5,以命名空间的方式来解析xml
示例xml:(这个xml文件中添加了两个命名空间,其中一个的前缀是fictional,另外一个作为默认的命名空间)
<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
xmlns="http://people.example.com">
<actor>
<name>John Cleese</name>
<fictional:character>Lancelot</fictional:character>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Eric Idle</name>
<fictional:character>Sir Robin</fictional:character>
<fictional:character>Gunther</fictional:character>
<fictional:character>Commander Clement</fictional:character>
</actor>
</actors>
方式一:在find()或者findall()接口中手动添加URI到每一个标签或者属性值前面。
root = fromstring(xml_text)
for actor in root.findall('{http://people.example.com}actor'):
name = actor.find('{http://people.example.com}name')
print name.text
for char in actor.findall('{http://characters.example.com}character'):
print ' |-->', char.text
方式二:在find()或者findall()接口中创建一个自己的前缀
ns = {'real_person': 'http://people.example.com',
'role': 'http://characters.example.com'}
for actor in root.findall('real_person:actor', ns):
name = actor.find('real_person:name', ns)
print name.text
for char in actor.findall('role:character', ns):
print ' |-->', char.text
两者的输出均为:
John Cleese
|--> Lancelot
|--> Archie Leach
Eric Idle
|--> Sir Robin
|--> Gunther
|--> Commander Clement
1 0
- python解析xml示例
- python xml解析之ElementTree 基本读操作示例
- Python解析XML正确应用代码示例讲解
- python xml解析之ElementTree 基本读操作示例
- JQuery解析xml示例
- XML解析示例
- document解析xml示例
- msxml解析xml示例
- dom4j 解析 XML 示例
- 85 XML解析示例
- XML pull解析 示例
- XML文件解析示例
- golang解析xml示例
- python解析文本文件示例
- jdom解析XML示例代码
- JOM4J解析XML数据示例
- struts2 示例struts.xml解析
- java-SAX解析XML示例
- java学习笔记9--内部类总结
- ThinkPHP 自动完成 和 自动验证
- google2016面试题-count of smaller numbers after self
- 校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛1007
- python系列之 RabbitMQ -- TOPICS
- python解析xml示例
- OCP试题
- 树莓派3配置局域网打印服务器惠普打印机HP1108
- 递归调用(实现汉诺塔)
- apt-get update 过期
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A B C
- 第十四讲--PGA管理和调整
- jdbc之c3p0笔记
- Viewpager的简单使用