xml 模块

xml 示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version='1.0' encoding='utf-8'?>
<namelist>
<name enrolled="yes">alex
<age checked="no">33</age>
<sex />
</name>
<name enrolled="yes">wpx
<age checked="no">55</age>
<sex />
</name>
<name enrolled="yes">chen
<age checked="no">66</age>
<sex />
</name>
</namelist>

遍历 xml 树:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import xml.etree.ElementTree as ET
tree= ET.parse("xml")
root = tree.getroot()
print(root)
#<Element 'namelist' at 0x0000020AEB4ECE58>
for child in root:
print(child.tag,child.attrib)
#name {'enrolled': 'yes'}
#name {'enrolled': 'yes'}
#name {'enrolled': 'yes'}
for child in root:
print(child.tag,child.attrib)
for i in child:
print(i.tag,i.text)
#name {'enrolled': 'yes'}
#age 33
#sex None
#name {'enrolled': 'yes'}
#age 55
#sex None
#name {'enrolled': 'yes'}
#age 66
#sex None

修改 xml 文本内容,属性内容

1
2
3
4
5
for node in  root.iter('age'):  #改文本内容
new_age = int(node.text)+1
node.text= str(new_age)
node.set("title",'yes') #改属性值
tree.write('xmltest.xml')

修改后 xmltest.xml 文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<namelist>
<name enrolled="yes">alex
<age checked="no" title="yes">34</age>
<sex />
</name>
<name enrolled="yes">wpx
<age checked="no" title="yes">56</age>
<sex />
</name>
<name enrolled="yes">chen
<age checked="no" title="yes">67</age>
<sex />
</name>
</namelist>

删除满足条件的 xml 数据

1
2
3
4
5
for name in root.findall('age'):
age = int(name.find('age').text)
if age > 50 :
root.remove()
tree.write('xml_test.xml')

新建 xml 树

1
2
3
4
5
6
7
8
9
new_xml = ET.Element('namelist')
name = ET.SubElement(new_xml,'name',attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
age.text='33'

et = ET.ElementTree(new_xml)
et.write("test.xml",encoding="utf-8",xml_declaration=True)
ET.dump(new_xml)

生成的 xml 文件代码如下

1
2
3
4
5
6
7
<?xml version='1.0' encoding='utf-8'?>
<namelist>
<name enrolled="yes">
<age checked="no">33</age>
<sex />
</name>
</namelist>