RSS Amplifier

c0ffee.me · Dec 11, 2012

Pretty Print XML

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

Put this python file in your path to get the following: #!/usr/bin/env python """ Command-line tool to validate and pretty-print XML. Based on `pjson` but without the crap. Usage:: $ echo ' <bunk atr= " hello " >world</bunk> ' | pxml <?xml version= " 1.0 " ?> <bunk atr= " hello " > world </bunk> Original Author: Igor Guerrero <igfgt1@gmail.com>, 2012 Contributor: Matthew Gill, 2012 """ import…

Put this python file in your path to get the following:

pretty printed xml

#!/usr/bin/env python

"""
Command-line tool to validate and pretty-print XML.
Based on `pjson` but without the crap.
Usage::
    $ echo '&lt;bunk atr="hello"&gt;world&lt;/bunk&gt;' | pxml
  &lt;?xml version="1.0" ?&gt;
  &lt;bunk atr="hello"&gt;
    world
  &lt;/bunk&gt;
Original Author: Igor Guerrero &lt;igfgt1@gmail.com&gt;, 2012
Contributor: Matthew Gill, 2012
"""
import xml.dom.minidom
import sys
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import XmlLexer
def format_xml_code(code):
    """
    Parses XML and formats it
    """
    x = xml.dom.minidom.parseString(code)
    return x.toprettyxml()
def color_yo_shit(code):
    """
    Calls pygments.highlight to color yo shit
    """
    return highlight(code, XmlLexer(), TerminalFormatter())
if __name__ == '__main__':
    code = format_xml_code(sys.stdin.read())
    print color_yo_shit(code)

The original can be found here (https://github.com/igorgue/pjson)

Read on /code/2012/12/11/pretty-print-xml.html

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.