【发布时间】:2016-05-13 12:24:17
【问题描述】:
我有一个如下所示的 XML 文件:
<Main>
<Stuff author="Jojo" name="Thing 1">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description" />
<Attr name="version" value="1.0.0" />
<Attr name="software" value="Misrocoft Ociffe" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Toto" name="Thing 2">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
</Main>
我正在更新它,然后重写它,但问题是如果我用prettyxml 重写它,我会在旧行之间有新的空格,如下所示:
<Main>
<Stuff author="Jojo" name="Thing 1">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description" />
<Attr name="version" value="1.0.0" />
<Attr name="software" value="Misrocoft Ociffe" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Toto" name="Thing 2">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Titi" name="New thing">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
</Main>
如果我重写它toxml,我根本就没有像这样的缩进或空格:
<Main>
<Stuff author="Jojo" name="Thing 1">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description" />
<Attr name="version" value="1.0.0" />
<Attr name="software" value="Misrocoft Ociffe" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Toto" name="Thing 2">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Titi" name="New thing"><Attr name="annotation" value="Short description" /><Attr name="description" value="Long description"/><Attr name="version" value="4.3.9" /><Attr name="software" value="Tophoshop" /><Attr name="language" value="Python" /><Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" /><Attr name="command" value="doSomething()" /></Stuff></Main>
有没有办法输出一个新的漂亮的 XML,它不会修改文件的现有格式?
我正在考虑将 XML 更改为单行字符串,然后在 prettyxml 中重新编写它,但我不知道该怎么做或者是否可能(我正在使用 etree 和 minidom 获取信息)。
更新(答案):
这是我最后制作的代码,注意我的rootXml来自ElementTree。
from xml.dom import minidom
import xml.etree.ElementTree as ET
def writeXml(rootXml, xmlFile):
roughString = ET.tostring(rootXml, 'utf-8')
oneLineString = ''.join([s.strip() for s in roughString.splitlines()])
minidomXml = minidom.parseString(oneLineString)
rootMinidom = minidomXml.firstChild
prettyXmlString = rootMinidom.toprettyxml()
prettyXml = ET.fromstring(prettyXmlString)
with open(xmlFile, "w") as f:
f.write (ET.tostring(prettyXml))
将返回以下 xml:
<Main>
<Stuff author="Jojo" name="Thing 1">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description" />
<Attr name="version" value="1.0.0" />
<Attr name="software" value="Misrocoft Ociffe" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Toto" name="Thing 2">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
<Stuff author="Titi" name="New thing">
<Attr name="annotation" value="Short description" />
<Attr name="description" value="Long description"/>
<Attr name="version" value="4.3.9" />
<Attr name="software" value="Tophoshop" />
<Attr name="language" value="Python" />
<Attr name="path" value="/here/there/aroundHere/somewhere/file.ext" />
<Attr name="command" value="doSomething()" />
</Stuff>
</Main>
【问题讨论】:
-
你在浪费时间。标签之间的空格并不重要。忽略它们
-
主要是为了可读性。 ^^
-
@e4c5 那么为什么要使用漂亮的 XML 呢?只需将其全部放入一个文件中,标签之间没有空格。这会让人类很容易准备好......
-
@busfault 哦,对不起,我没有意识到当其他人阅读漫画时,还有人真正阅读 xml
-
@e4c5 我猜您从未有过尝试调试其他人的混乱 XML 的乐趣。您的代码必须非常易于调试。
标签: python xml elementtree minidom