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

5

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

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

我不认为它会改变,因为我已经进行了简单的读写测试,没有做任何修改。然而,结果显示了一个不同的故事,特别是在命名空间索引方面(nonage --> ns0,d3p1 --> ns1,i --> ns2):

input.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>

和output.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>

1个回答

6

在使用ElementTree.register_namespace函数读写xml之前,您需要向ElementTree注册xml的命名空间以及它们的前缀。例如 -

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将为相应的命名空间创建自己的前缀,这就是您的情况发生的情况。

这在文档中给出 -

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

注册命名空间前缀。注册表是全局的,并且将删除任何现有映射,无论是给定前缀还是命名空间URI。前缀是命名空间前缀。uri是命名空间uri。如果可能的话,此命名空间中的标签和属性将使用给定的前缀进行序列化。

(重点是我的)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接