gpt4 book ai didi

python - 如何从 Element 获取原始 xml 作为字符串

转载 作者:数据小太阳 更新时间:2023-10-29 02:28:38 24 4
gpt4 key购买 nike

我有以下代码解析 XML 响应并返回表示产品的 Element 对象列表。

tree =  ET.ElementTree(ET.fromstring(raw_xml_response))
products = root.findall('//Product')

我现在需要从每个产品中获取原始 XML,以便将其存储在数据库中。似乎 Element 没有执行此操作的方法 - 从每个 Product Element 获取原始 XML 的惯用方法是什么?

谢谢,

最佳答案

有一个名为tostring ( docs ) 的顶层函数接受一个元素,试试

print([ET.tostring(product) for product in products])

关于python - 如何从 Element 获取原始 xml 作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49049996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com