I've got a XML file which looks like this:
<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>
I'm updating it and then rewriting it but the problem is that if I rewriting it with prettyxml
I've got new spaces between the old lines like this:
<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>
And if I rewrite it toxml
I've got no indentations or spaces at all like this:
<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>
Is there a way to output a new pretty XML which will not modify the existing format of the file?
I was thinking of changing the XML to a one-line string and then re-writing it in prettyxml but I don't know how to do it or if it is possible (I'm using etree
and minidom
for info).
Update (answer):
Here is the code I finally made, note that my rootXml
is from 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))
Will return the following 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>