我有一个类似如下的XML文件:
<ns0:component xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685-
2014" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2014
http://www.accellera.org/XMLSchema/IPXACT/1685-2014/index.xsd">
<ns0:vendor>spiritconsortium.org</ns0:vendor>
<ns0:library>Leon2RTL</ns0:library>
<ns0:name>ahbbus12</ns0:name>
<ns0:circuitFunctionDescriptions><ns0:name>PLL</ns0:name>
</ns0:circuitFunctionDescriptions></ns0:component>
但是我想要一个漂亮的表单,比如:
<ns0:component xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685-
2014" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2014
http://www.accellera.org/XMLSchema/IPXACT/1685-2014/index.xsd">
<ns0:vendor>spiritconsortium.org</ns0:vendor>
<ns0:library>Leon2RTL</ns0:library>
<ns0:name>ahbbus12</ns0:name>
<ns0:circuitFunctionDescriptions>
<ns0:name>PLL</ns0:name>
</ns0:circuitFunctionDescriptions>
</ns0:component>
我需要将其保存为输出文件(我的意思是,我需要将结果保存到另一个xml文件中,而不仅仅是打印它)。我想知道我该怎么做?
发布于 2018-08-10 08:43:31
大多数具有美观的缩进功能的序列化程序都会提供类似于您所寻找的元素缩排的内容。查看,因为您已经用python标记了您的问题。
有些还会提供属性缩进。
但是,它们都不会像在您期望的XML中那样中断属性值,因为通常不可能在属性值中添加空格,同时仍然可以确保语义将被保留。
您可以在您的示例中看到这一点。
这个名称空间声明(即使我们很慷慨并从源文档中修复它),
xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685-2014"
不同于
xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685-
2014"
https://stackoverflow.com/questions/51777141
复制相似问题