Created
September 30, 2010 06:50
-
-
Save andrewwatts/604142 to your computer and use it in GitHub Desktop.
a python xmltool in the spirit of python's json.tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
In the spirit of python's json.tool, provide a command line | |
utility to pretty print XML. | |
Usage (): | |
$ echo "<book><title>Oliver Twist</title></book>" | python -m xmltool | |
<?xml version="1.0" encoding="utf-8"?> | |
<book> | |
<title> | |
Oliver Twist | |
</title> | |
</book> | |
$ curl -s http://api.twitter.com/1/help/test.xml | python -m xmltool | |
<?xml version="1.0" encoding="utf-8"?> | |
<ok> | |
true | |
</ok> | |
$ python xmltool.py infile.xml | |
$ python xmltool.py infile.xml outfile.xml | |
""" | |
import sys | |
from xml.dom import minidom | |
def main(): | |
if len(sys.argv) == 1: | |
infile = sys.stdin | |
outfile = sys.stdout | |
elif len(sys.argv) == 2: | |
infile = open(sys.argv[1], 'rb') | |
outfile = sys.stdout | |
elif len(sys.argv) == 3: | |
infile = open(sys.argv[1], 'rb') | |
outfile = open(sys.argv[2], 'wb') | |
else: | |
raise SystemExit(sys.argv[0] + ' [infile [outfile]]') | |
try: | |
dom = minidom.parse(infile) | |
except Exception as e: | |
raise SystemExit(e) | |
prettyxml = dom.toprettyxml(indent=' '*4, encoding='utf-8') | |
outfile.write(prettyxml) | |
outfile.write('\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment