gpt4 book ai didi

python - 防止 ElementTree 为每个元素添加命名空间

转载 作者:行者123 更新时间:2023-12-04 16:09:25 24 4
gpt4 key购买 nike

我在 Python 中使用 ElementTree 来解析 xml 文件并在其中添加或删除元素。

在我的 XML 文件中,根和根下方的元素有一个命名空间,但所有其他元素都没有。

我看到 ElementTree 在打印修改后的树时,为每个元素添加了命名空间。

是否有正确的方法将 ElementTree 告诉 只需将命名空间保留在它们最初出现的元素中 ?

最佳答案

试试这个:

import xml.etree.ElementTree as ET
namespaces = {
'': 'http://tempuri.org/',
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsd': 'http://www.w3.org/2001/XMLSchema',
}
for prefix, uri in namespaces.items():
ET.register_namespace(prefix, uri)

关于python - 防止 ElementTree 为每个元素添加命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433631/

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