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示例🏁 :
<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>
tree = ET.ElementTree(file=xml_file)
root = tree.getroot()
属性 | 类型 | 描述 |
---|---|---|
attrib | 字典 | 元素下key-value模式的属性值 |
tag | 字符串 | 元素存储的数据类型 |
text | 字符串 | 元素的文本值 |
<RoomNum num="desc">4</RoomNum>
tag attrib text tag
ele.set('kye', new-vlue)
tree.write(xml_file, xml_declaration=False)
以上面的xml为例
UniqueID
的ID值UniqueID = tree.iter('UniqueID')
for ele in UniqueID:
ele.set('ID', 'test20240104')
PromotionInfo = tree.iter('PromotionInfo')
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')
RoomNumE = tree.iter('RoomNum')
for ele in RoomNumE:
ele.text = '2'
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。