码迷,mamicode.com
首页 > 编程语言 > 详细

python - 常用模块 - xml处理模块

时间:2017-04-08 17:18:28      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:分享   for   nod   写入   行数据   update   count   返回   isp   

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在以前,在json还没诞生之前,xml十分流行,

以至于到现在很多传统公司如金融行业的很多系统的接口还主要是xml。

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 
 5 import xml.etree.ElementTree as ET
 6 
 7 print(‘‘‘ 
 8      =============打印根节点的名字============
 9     ‘‘‘)
10 tree = ET.parse("./file/test.xml")    #指定要解析的xml文件
11 root = tree.getroot()   #根节点里面的所有内容
12 print(root.tag)      #打印xml文件根节点的名字,eg:./file/test.xml中的data
13 
14 print(‘‘‘ 
15      =============遍历整个xml文档============
16     ‘‘‘)
17 #遍历xml文档   .tag:节点名   .attrib: 节点属性   .text: 节点内容
18 for child in root:      #打印根节点下的第一层子节点 country
19     print(child.tag, child.attrib)
20     for i in child:      #打印第一层子节点下的子节点(第二层子节点)
21         print(i.tag,i.text)
22 
23 print(‘‘‘ 
24      =============只遍历year 节点============
25     ‘‘‘)
26 #只遍历year 节点   
27 # root.iter(‘year‘): 在./file/test.xml中选择出所有节点名为‘year‘的节点
28 # .iter(‘year‘)  返回一个迭代器包含所有匹配的元素。
29 for node in root.iter(year):
30     print(node.tag,node.text)
31 
32 print(‘‘‘
33     =============修改xml文档============
34 ‘‘‘)
35 #修改xml文档,把所有year节点的值+1
36 for node in root.iter(year):
37     new_year = int(node.text) + 1
38     node.text = str(new_year)
39     node.set("updated","yes")    #给节点增加属性
40  
41 tree.write("./file/test1.xml")   #对文档做修改后要进行写入,才能保存修改
42 print("修改结果看./file/test1.xml") 
43 
44 print(‘‘‘
45     =============删除xml文档节点============
46 ‘‘‘)
47 #删除指定node
48 #for country in root.iter(‘country‘):   可以有同样效果,原理不同
49 for country in root.findall(country):
50     print(country.tag,country.attrib)
51     rank = int(country.find(rank).text)
52     if rank > 50:
53         root.remove(country)
54  
55 tree.write(./file/test2.xml)    #对文档做修改后要进行写入,才能保存修改
56 print("删除后的结果看./file/test1.xml") 
57 
58 
59 
60 print(=============创建xml文档============)
61 
62 import xml.etree.ElementTree as ET
63 
64 new_xml = ET.Element("namelist")  #定义根节点
65 name = ET.SubElement(new_xml,"name1",attrib={"enrolled":"yes"})
66 age = ET.SubElement(name,"age",attrib={"checked":"no"})
67 sex = ET.SubElement(name,"sex")
68 age.text = 33
69 sex.text = male
70 name2 = ET.SubElement(new_xml,"name2",attrib={"enrolled":"no"})
71 age = ET.SubElement(name2,"age")
72 sex = ET.SubElement(name2,"sex")
73 age.text = 19
74 sex.text = female
75 
76 et = ET.ElementTree(new_xml) #生成文档对象
77 et.write("./file/test3.xml", encoding="utf-8",xml_declaration=True)
78 
79 #ET.dump(new_xml) #打印生成的格式在屏幕
80 
81 print(创建后结果看./file/test3.xml)

上面涉及到文件:

技术分享
 1 <?xml version="1.0"?>
 2 <data>
 3     <country name="Liechtenstein">
 4         <rank updated="yes">2</rank>
 5         <year>2008</year>
 6         <gdppc>141100</gdppc>
 7         <neighbor name="Austria" direction="E"/>
 8         <neighbor name="Switzerland" direction="W"/>
 9     </country>
10     <country name="Singapore">
11         <rank updated="yes">5</rank>
12         <year>2011</year>
13         <gdppc>59900</gdppc>
14         <neighbor name="Malaysia" direction="N"/>
15     </country>
16     <country name="Panama">
17         <rank updated="yes">69</rank> 
18         <year>2011</year>
19         <gdppc>13600</gdppc>
20         <neighbor name="Costa Rica" direction="W"/>
21         <neighbor name="Colombia" direction="E"/>
22     </country>
23 </data>
test.xml
技术分享
 1 <data>
 2     <country name="Liechtenstein">
 3         <rank updated="yes">2</rank>
 4         <year updated="yes">2009</year>
 5         <gdppc>141100</gdppc>
 6         <neighbor direction="E" name="Austria" />
 7         <neighbor direction="W" name="Switzerland" />
 8     </country>
 9     <country name="Singapore">
10         <rank updated="yes">5</rank>
11         <year updated="yes">2012</year>
12         <gdppc>59900</gdppc>
13         <neighbor direction="N" name="Malaysia" />
14     </country>
15     <country name="Panama">
16         <rank updated="yes">69</rank> 
17         <year updated="yes">2012</year>
18         <gdppc>13600</gdppc>
19         <neighbor direction="W" name="Costa Rica" />
20         <neighbor direction="E" name="Colombia" />
21     </country>
22 </data>
test1.xml
技术分享
 1 <data>
 2     <country name="Liechtenstein">
 3         <rank updated="yes">2</rank>
 4         <year updated="yes">2009</year>
 5         <gdppc>141100</gdppc>
 6         <neighbor direction="E" name="Austria" />
 7         <neighbor direction="W" name="Switzerland" />
 8     </country>
 9     <country name="Singapore">
10         <rank updated="yes">5</rank>
11         <year updated="yes">2012</year>
12         <gdppc>59900</gdppc>
13         <neighbor direction="N" name="Malaysia" />
14     </country>
15     </data>
test2.xml
技术分享
1 <?xml version=1.0 encoding=utf-8?>
2 <namelist><name1 enrolled="yes"><age checked="no">33</age><sex>male</sex></name1><name2 enrolled="no"><age>19</age><sex>female</sex></name2></namelist>
test3.xml

 

python - 常用模块 - xml处理模块

标签:分享   for   nod   写入   行数据   update   count   返回   isp   

原文地址:http://www.cnblogs.com/xtsec/p/6682113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!