Pretty Printing XML in Python
MartinPacker 11000094DH Comments (2) Visits (7799)
A need has arisen for pretty printing XML. This post includes some Python code to do it.
I've been working with the OpenOffice.org ODP presentation file format recently: I want to generate presentations from application code.
An ODP file is a structured zip file. Among other items it contains an XML file - content.xml - which describes the pages in the presentation. content.xml concatenates everything as one long line of text. It's therefore very hard to read - without specialist XML parsing tools. And I'd like to - so I can teach my code to navigate and update it.
So the following is a short Python program to pretty print an XML file. It takes the XML file as standard input (stdin) and writes the formatted XML to standard output (stdout). If it can't parse the XML it writes an appropriate error message to standard error (stderr).
While this isn't a Python tutorial I'd like to outline how it works:
I'll admit to a little disappointment that toprettyxml() doesn't create quite as pretty an output file as I'd like. Hence the post-processing. Feel free to experiment with different values for the parameters to it. Also you might not feel the need for some of the post-processing.
Here's the code. It's more white space, comments and error handling than actual straight-through processing.
One question: Can this be run on z/OS? I believe it can: minidom is a core part of Jython and Jython certainly runs under z/OS. In fact, being a java jar I'd expect it to run on a zAAP (or zIIP with zAAP-on-zIIP). If you try it and get it working let us know.