I am trying to print XML out of an Element object, so that formatting allows us to print the tag attributes in new line.
elem = etree.Element() //Some element
str = etree.tostring(elem, pretty_print=True)
The current output looks like
<module name="A" description="abc" type="xyz">
<container/>
</module>
Formatting needed
<module
name="A"
description="abc"
type="xyz">
<container/>
</module>
Is there any existing library that allows us to print newlines for all the attributes present in the tags.
Etree cannot format attributes like that to my knowledge.
Alternatively, you could try tidylib(http://www.html-tidy.org/) to do the formatting.
On Ubuntu you can do:
sudo apt install tidy
sudo pip install tidylib
Then to format with each attribute on a new line try something like:
>>> from tidylib import tidy_document
>>> k = """<module name="A" description="abc" type="xyz">
<container/>
</module>
"""
>>> document, errors = tidy_document(k, options={'indent-attributes':'yes', 'input-xml':'yes'})
>>> print(document)
<module name="A"
description="abc"
type="xyz">
<container />
</module>
Tidy is a vast library with many features and you can read more about the indent-attributes
feature here: http://api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With