在web数据传输过程中,用到最多的两种数据传输格式分别是json和xml,现记录一下如何使用python的ElementTree库来操作xml,实现对xml的增删查改!
说明:
这里通过以下方式来操作xml
xml数据样例:
<root name="test" msg="测试">
<head>
<uid>6taE112M48343D7QaP452o29uid>
<fileType>OtherfileType>
<sum>100sum>
<createTime>2018-11-28 15:10:02createTime>
head>
<datas>
<data>
<id>1530id>
<osId>UNIX1001osId>
<ip>192.168.1.2ip>
<groupId>275groupId>
<secrity>安全secrity>
<securityManager>张三securityManager>
<port>22port>
<protocol>sshprotocol>
<upPriv>su -upPriv>
data>
<data>
<id>1531id>
<osId>UNIX1002osId>
<ip>192.168.1.3ip>
<groupId>276groupId>
<secrity>安全secrity>
<securityManager>李四securityManager>
<port>25port>
<protocol>sshprotocol>
<upPriv>insertupPriv>
data>
datas>
root>
#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
# 如果你依然在编程的世界里迷茫,
# 不知道自己的未来规划,
# 对python感兴趣,
这里推荐一下我的学习交流圈:895 797 751,
# 里面都是学习python的,
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)
# 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)
# 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
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)
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)
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
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)