'XML ElementTree with pretty print
1.) if you look at the following tags and the spaces of my xml-output you will notice that the spaces are not that correct, cause of the code in 2.)
<SD-ID-Gesamt>&</SD-ID-Gesamt><Lage>&</Lage><Parameter>&</Parameter>
Current output:
<SD-ID-Gesart>
<SD-ID-Code>
<SD-ID>DD-RE-1.0G-10</SD-ID>
<Lage>
<XP>14.84</XP>
<YP>73.19</YP>
<ZP>7.92</ZP>
<WKS>95.32</WKS>
</Lage>
<Parameter>
<Form>Rectangle</Form>
<Art>Deckendurchbruch/Art>
<Gewerk>-HT-SAN-DURCHBRUCH</Gewerk>
<Ebene>1.OG OKFF +4,50 m</Ebene>
<Breite>1,800000</Breite>
<Höhe>0,500000</Höhe>
<Tiefe>0,450000</Tiefe>
<Status_Neu>0</Status_Neu>
<Status_Geändert>1</Status_Geändert>
</Parameter>
<SD-ID-Code>
2.) I am calling indent() to pretty print my xml-files. It is working almost as wished, except from one little mistake in the code you see below. The mistake you can see in the xml-output in the picture above.
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + ""
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
3.) with the code you see next i create the ElementTree, when it went through indent().
indent(sd)
tree = ET.ElementTree(sd)
tree.write(open(pfad + '\\'+ datei_name + '.xml', "w"), encoding='utf-8',
xml_declaration=True, method='xml')
I need to edit the definition of the indent() indent function to get the spaces correct. In that case I really have no idea how to put back the spaces when the tag is equal. I would be very very happy if you could help me getting that fixed.
Solution 1:[1]
Update: See xml.etree.ElementTree.indent as of Python 3.9.
I couldn't reproduce the bad indentation from your example, but according to http://effbot.org/zone/element-lib.htm#prettyprint, your function is mis-copied. For these lines:
if not elem.text or not elem.text.strip():
elem.text = i + ""
There should be two space between the quotes:
if not elem.text or not elem.text.strip():
elem.text = i + " "
I ran this code and it displays properly.
from xml.etree import ElementTree as et
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for e in elem:
indent(e, level+1)
if not e.tail or not e.tail.strip():
e.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
data = '''<one><two><three>3</three><four>4</four></two></one>'''
tree = et.fromstring(data)
indent(tree)
et.dump(tree)
Output:
<one>
<two>
<three>3</three>
<four>4</four>
</two>
</one>
Notes for future:
- Images of text can't be copied so I leave it as an exercise for you to test on your own XML.
- Cut-n-paste exact code and input data as text to reproduce the issue to make it easy for answers to reproduce the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
