Luis Cruz
Luis Cruz

Reputation: 403

Split long XML tags in multiple lines with lxml

My python (2.7) script is outputting the following XML using lxml library:

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="17dp" android:layout_marginTop="16dp" android:text="Button"/>

I would like to output it in multiple lines, one per attribute:

<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="17dp"
  android:layout_marginTop="16dp"
  android:text="Button" />

Upvotes: 2

Views: 770

Answers (1)

Luis Cruz
Luis Cruz

Reputation: 403

I came up with a very naive and inefficient approach.

After generating the xml using lxml library, I process the output. The following code is only tested to work with lxml output and assumes we have a tag per line.

output = etree.tostring(
    tree,
    xml_declaration=True,
    pretty_print=True,
    encoding=tree.docinfo.encoding,
)
output = output.replace("\" ","\"\n")
with open(filename, "w") as f:
    parent_tag_line = None
    for i, line in enumerate(output.splitlines()):
        line_stripped = line.lstrip(" ")
        line_ident = len(line) - len(line_stripped)
        if parent_tag_line is not None:
            if line_ident == 0 and line[:2] != "</":
                line = (parent_line_ident+2)*" " + line
            else:
                parent_tag_line = line
                parent_line_ident = line_ident
                line_stripped = line.lstrip()
                if line_stripped[:4] != "<!--" and line_stripped[:2] != "</":
                    line="\n"+line
        else:
            parent_tag_line = line
            parent_line_ident = line_ident
        print >>f, line

Although this gets the job done, it is far from being an optimal approach. I wonder if there's a better and simpler way of doing this.

Upvotes: 2

Related Questions