1

I'm using BS4 in some Python to make a raw string look like good looking XML.

I'm using this:

fileText = (BeautifulSoup(fileText, "xml").prettify())

It gives me output like this:

<foobar>
  <foo>
  bar
  </foo>
  <foo>
  bar2
  </foo>
</foobar>

but i'd like:

<foobar>
  <foo>bar</foo>
  <foo>bar2</foo>
</foobar>

Any help is greatly appreciated!

Leicham
  • 31
  • 2

1 Answers1

0

From Jayesh Bhoot's answer:

from lxml import etree, html

doc = html.fromstring(fileText)
print(etree.tostring(doc, encoding='unicode', pretty_print=True))

And as per dspjm's comment to the answer linked above, this works just as well:

print(html.tostring(doc, encoding='unicode', pretty_print=True, method='xml'))

The only condition is that method='xml' is required when using html.tostring.

The output:

<foobar>
  <foo>bar</foo>
  <foo>bar2</foo>
</foobar>
Rob
  • 26,989
  • 16
  • 82
  • 98