前言:
在web数据传输过程中,用到最多的两种数据传输格式分别是json和xml,现记录一下如何使用python的ElementTree库来操作xml,实现对xml的增删查改!
说明:
这里通过以下方式来操作xml
- 标签节点的CRUD
- 标签属性的CRUD
- 标签值的CRUD
xml数据样例:
<?xml version='1.0' encoding='utf-8'?>
<root name="test" msg="测试">
<head>
<uid>6taE112M48343D7QaP452o29</uid>
<fileType>Other</fileType>
<sum>100</sum>
<createTime>2018-11-28 15:10:02</createTime>
</head>
<datas>
<data>
<id>1530</id>
<osId>UNIX1001</osId>
<ip>192.168.1.2</ip>
<groupId>275</groupId>
<secrity>安全</secrity>
<securityManager>张三</securityManager>
<port>22</port>
<protocol>ssh</protocol>
<upPriv>su -</upPriv>
</data>
<data>
<id>1531</id>
<osId>UNIX1002</osId>
<ip>192.168.1.3</ip>
<groupId>276</groupId>
<secrity>安全</secrity>
<securityManager>李四</securityManager>
<port>25</port>
<protocol>ssh</protocol>
<upPriv>insert</upPriv>
</data>
</datas>
</root>
标签节点的CRUD
1.查询标签节点
1.findall(标签名):查找所有标签节点,返回一个可迭代对象
2.find(标签名):查找满足条件的第一个标签节点,返回该节点对象
#coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def select_node():
"""查询节点"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# findall():查找并迭代标签
for data in root.findall('.//data'): # xpath语法
print data
# find(): 查找满足条件的第一个标签
data = root.find('.//data')
print data
2.删除标签节点
1.remove(节点对象):删除指定的节点
2.clear():清空本节点下的所有子节点
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def del_node():
"""添加节点"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 获取datas节点
datas_node = root.find(".//datas")
# 获取datas的第一个子节点
data_node = root.find('.//datas/data')
# 1.remove(节点对象):删除指定的节点
datas_node.remove(data_node)
# 2.clear(): 清空datas下的所有子节点
# datas_node.clear()
# 回写xml数据
tree.write("test2.xml", encoding='utf-8', xml_declaration=True)
3.添加标签节点:(需要两步:创建标签节点,添加标签)
1.创建标签节点:
Element(标签名):创建标签节点对象
2.添加标签:
1.append(标签节点):在标签的末尾添加新标签
2.insert(索引号,标签节点):在指定的索引位置添加新标签
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def add_node():
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# Element(标签名):创建标签节点对象
new_node = ET.Element("node")
# 添加标签值
new_node.text = "newNode"
# 添加标签
root.append(new_node)
# 回写xml数据
tree.write("test2.xml", encoding='utf-8', xml_declaration=True)
标签属性的CRUD
1.获取标签属性:
1.attrib:以字典形式返回标签属性
2.keys():返回标签属性名称列表
3.items():返回标签属性项列表
4.get(key, default=None):根据标签属性名称获取标签属性值
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def select_attrib():
"""查询标签属性"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 获取标签属性
attr_dict = root.attrib # {'msg': '测试', 'name': 'test'}
attr_key = root.keys() # ['msg', 'name']
attr_item = root.items() # [('msg', u'测试'), ('name', 'test')]
attr_get = root.get('name') # test
2.添加/修改标签属性:
set(key, value):添加/修改标签属性
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def update_attrib():
"""添加/修改标签属性"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 不存在则添加
root.set("age", "30")
# 存在则修改
root.set("name", "mytest")
# 回写xml数据
tree.write("test2.xml", encoding='utf-8', xml_declaration=True)
3.删除标签属性:
del node.attrib[key]
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def del_attrib():
"""删除节点属性"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 删除名为name的属性
del root.attrib["name"]
# 回写xml数据
tree.write("test2.xml", encoding='utf-8', xml_declaration=True)
标签值的CRUD
1.获取标签值
node.text
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def select_node_value():
"""查询节点值"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 获取第一个ip节点
ip_node = root.find(".//ip")
# 获取节点值
node_value = ip_node.text
print node_value
2.添加/修改标签值
node.text = value :存在就修改,不存在就添加
# coding:utf-8
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def update_node_value():
"""添加/修改标签属性"""
xml_path = "test.xml"
# 通过获取tree对象
tree = ET.parse(xml_path)
# 获取根节点
root = tree.getroot()
# 获取第一个ip节点
ip_node = root.find(".//ip")
# 修改节点值
ip_node.text = "10.1.1.1"
# 回写xml数据
tree.write("test2.xml", encoding='utf-8', xml_declaration=True)
网友评论