python使用ElementTree操作xml

前言:

在web数据传输过程中,用到最多的两种数据传输格式分别是json和xml,现记录一下如何使用python的ElementTree库来操作xml,实现对xml的增删查改!

说明:
这里通过以下方式来操作xml

  • 标签节点的CRUD
  • 标签属性的CRUD
  • 标签值的CRUD

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>

标签节点的CRUD

  1. 查询标签节点
  2. findall(标签名):查找所有标签节点,返回一个可迭代对象
  3. 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
# 如果你依然在编程的世界里迷茫,
# 不知道自己的未来规划,
# 对python感兴趣,
这里推荐一下我的学习交流圈:895 797 751# 里面都是学习python的,

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)

你可能感兴趣的:(Python进阶之路,Django,Python,Python爬虫)