Python使用ElementTree解析xml

Python
199
0
0
2024-03-16
标签   Python库

ElementTree 是Python用来解析和处理 XML的标准库,它提供了轻量级的 Python 式的 API ,它由一个 C 实现来提供。

如何引入

ElementTree生来就是为了处理 XML ,它在 Python 标准库中有两种实现。一种是纯 Python 实现例如xml.etree.ElementTree,另外一种是速度快一点的xml.etree.cElementTree。 尽量使用 C 语言实现的那种,因为它速度更快,而且消耗的内存更少。

try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET

XML 解析

🏁 xml示例🏁 :

<HotelReservations>
    <HotelReservation>
      <UniqueID ID="test20231703489561" />
      <RoomStays>
        <RoomStay>
                <Rate>
                  <Base BaseAmountOriginal="113.1" SaleAmountOriginal="377.0">
                    <PromotionInfo BaseDiscountAmount="188.5" PromotionId="1000000519" RuleId="188552516469001" SaleDiscountAmount="188.5" />
                    <PromotionInfo BaseDiscountAmount="75.4" PromotionId="1000000485" RuleId="222734520418530" SaleDiscountAmount="75.4" />
                  </Base>
                </Rate>
              </Rates>
            </RoomRate>
          </RoomRates>
        </RoomStay>
      </RoomStays>
      <ResGlobalInfo>
        <RoomNum num="desc">4</RoomNum>
      </ResGlobalInfo>
    </HotelReservation>
  </HotelReservations>
  1. 加载并解析指定XML
tree = ET.ElementTree(file=xml_file)
  1. 获取根节点
root = tree.getroot()
  1. 解析模块

属性

类型

描述

attrib

字典

元素下key-value模式的属性值

tag

字符串

元素存储的数据类型

text

字符串

元素的文本值

  1. 属性对应值
<RoomNum num="desc">4</RoomNum>
    tag    attrib  text  tag
  1. 修改值
ele.set('kye', new-vlue)
  1. 更新文件
tree.write(xml_file, xml_declaration=False)

实战应用

以上面的xml为例

  1. 修改唯一元素值:UniqueID的ID值
UniqueID = tree.iter('UniqueID')
for ele in UniqueID:
      ele.set('ID', 'test20240104')
  1. 遍历修改元素值:PromotionInfo的BaseDiscountAmount值
  •  🍀通过 iter() 方法来遍历所有元素,找到 PromotionInfo
PromotionInfo = tree.iter('PromotionInfo')
  •  🍀根据attrib属性,查找key,修改value值 iter(tag=None) 以当前元素为根节点,创建树迭代器
for ele in PromotionInfo:
	#attrib为字典类型
        attrib_dict = ele.attrib

        # 查找key,修改value
        if "300810237903009" in attrib_dict.values():
            ele.set('BaseDiscountAmount', '100')
            ele.set('SaleDiscountAmount', '100')
  1. 修改text值
RoomNumE = tree.iter('RoomNum')
    for ele in RoomNumE:
        ele.text = '2'