Python 模块 xml.etree.ElementTree 自动修改 xml 命名空间键

标签 python xml elementtree

我注意到 python ElementTree 模块在以下简单示例中更改了 xml 数据:

import xml.etree.ElementTree as ET
tree = ET.parse("./input.xml")
tree.write("./output.xml")

我不希望它发生变化,因为我已经完成了简单的读写测试,没有进行任何修改。然而,结果显示了不同的故事,尤其是在 namespace 索引中(nonage --> ns0 , d3p1 --> ns1 , i --> ns2 ):

输入.xml:

<?xml version="1.0" encoding="utf-8"?>
<ServerData xmlns:i="http://www.a.org" xmlns="http://schemas.xxx/2004/07/Server.Facades.ImportExport">
<CreationDate>0001-01-01T00:00:00</CreationDate>
<Processes>
    <Processes xmlns:d3p1="http://schemas.datacontract.org/2004/07/Management.Interfaces">
        <d3p1:ProtectedProcess>
            <d3p1:Description>/Applications/Safari.app/Contents/MacOS/Safari</d3p1:Description>
            <d3p1:DiscoveredMachine i:nil="true" />
            <d3p1:Id>0</d3p1:Id>
            <d3p1:Name>/applications/safari.app/contents/macos/safari</d3p1:Name>
            <d3p1:Path>/Applications/Safari.app/Contents/MacOS/Safari</d3p1:Path>
            <d3p1:ProcessHashes xmlns:d5p1="http://schemas.datacontract.org/2004/07/Management.Interfaces.WildFire" />
            <d3p1:Status>1</d3p1:Status>
            <d3p1:Type>Protected</d3p1:Type>
        </d3p1:ProtectedProcess>
    </Processes>
</Processes>

和输出.xml:

<ns0:ServerData xmlns:ns0="http://schemas.xxx/2004/07/Server.Facades.ImportExport" xmlns:ns1="http://schemas.datacontract.org/2004/07/Management.Interfaces" xmlns:ns2="http://www.a.org">
<ns0:CreationDate>0001-01-01T00:00:00</ns0:CreationDate>
<ns0:Processes>
    <ns0:Processes>
        <ns1:ProtectedProcess>
            <ns1:Description>/Applications/Safari.app/Contents/MacOS/Safari</ns1:Description>
            <ns1:DiscoveredMachine ns2:nil="true" />
            <ns1:Id>0</ns1:Id>
            <ns1:Name>/applications/safari.app/contents/macos/safari</ns1:Name>
            <ns1:Path>/Applications/Safari.app/Contents/MacOS/Safari</ns1:Path>
            <ns1:ProcessHashes />
            <ns1:Status>1</ns1:Status>
            <ns1:Type>Protected</ns1:Type>
        </ns1:ProtectedProcess>
    </ns0:Processes>
</ns0:Processes>

最佳答案

在使用 ElementTree.register_namespace 读取/写入 xml 之前,您需要为 xml 注册命名空间及其前缀 ElementTree功能。示例 -

import xml.etree.ElementTree as ET

ET.register_namespace('','http://schemas.xxx/2004/07/Server.Facades.ImportExport')
ET.register_namespace('i','http://www.a.org')
ET.register_namespace('d3p1','http://schemas.datacontract.org/2004/07/Management.Interfaces')

tree = ET.parse("./input.xml")
tree.write("./output.xml")

没有这个 ElementTree 会为相应的命名空间创建自己的前缀,这就是您的情况。

这在 documentation 中给出-

xml.etree.ElementTree.register_namespace(prefix, uri)

Registers a namespace prefix. The registry is global, and any existing mapping for either the given prefix or the namespace URI will be removed. prefix is a namespace prefix. uri is a namespace uri. Tags and attributes in this namespace will be serialized with the given prefix, if at all possible.

(强调我的)

关于Python 模块 xml.etree.ElementTree 自动修改 xml 命名空间键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33258826/

相关文章:

sql - 具有动态命名空间的 Oracle extractvalue

javascript - 在前端抓取从 Django 发送的文件

xml - Nmap::Parser,大文件的段错误

Python - 分类的最低方差的箱大小

python - 向列表中添加值

python - Tkinter Canvas 存储的项目被清除和召回

python - 使用元素树读取动态xml

python - 为什么map_async()不需要pool.close()和pool.join()?

python - 在python中向XML添加子元素

python elementtree空白输出