Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing each attribute with newline using Element from lxml

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.

like image 777
arpp Avatar asked Nov 08 '22 08:11

arpp


1 Answers

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

like image 134
Martin Gergov Avatar answered Nov 15 '22 12:11

Martin Gergov