views:

40

answers:

2

I generate a long and ugly XML string with python, and I need to filter it through pretty printer to look better.

I found this post for python pretty printers, but I have to write the XML string to a file to be read back to use the tools, which I want to avoid if possible.

What python pretty tools that works on string are available?

+4  A: 

I use the lxml library, and there it's as simple as

>>> print(etree.tostring(root, pretty_print=True))

You can do that operation using any etree, which you can either generate programmatically, or read from a file.

If you're using the DOM from PyXML, it's

import xml.dom.ext
xml.dom.ext.PrettyPrint(doc)

That prints to the standard output, unless you specify an alternate stream.

http://pyxml.sourceforge.net/topics/howto/node19.html

To directly use the minidom, you want to use the toprettyxml() function.

http://docs.python.org/library/xml.dom.minidom.html#xml.dom.minidom.Node.toprettyxml

Paul McMillan
@Paul : It looks like that both root and doc are structured data, not a string. Thanks for the answer.
prosseek
If your xml exists as a minidom node, you can use the `toprettyxml()` function. If it really only ever exists as a string, you will have to parse it in before you can pretty print it out.
Paul McMillan
+2  A: 

Here's how to parse from a text string to the lxml structured data type.

from lxml import etree
xml_str = "<parent><child>text</child><child>other text</child></parent>"
root = etree.fromstring(xml_str)
print etree.tostring(root, pretty_print=True)

Outputs:

<parent>
  <child>text</child>
  <child>other text</child>
</parent>
monkut