Skip to content

Instantly share code, notes, and snippets.

@xlson
Created January 18, 2011 16:13
Show Gist options
  • Save xlson/784665 to your computer and use it in GitHub Desktop.
Save xlson/784665 to your computer and use it in GitHub Desktop.
Pretty-prints XML on the commandline (requires groovyserv)
#!/usr/bin/env groovyclient
println(prettyPrintXml(System.in.text))
def prettyPrintXml(text) {
def xmlNode = new XmlParser().parseText(text)
def writer = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.preserveWhitespace = true
printer.print(xmlNode)
writer.toString().trim()
}
@xlson
Copy link
Author

xlson commented Jan 18, 2011

ppxml - Pretty-print XML on the command-line

Usage

echo '<person><name>Leonard</name></person>' | ppxml

Output:

Leonard

Installation & Deps

Put it on your path and install groovyserv.

Alternative: If you don't want groovyserv you can use ordinary groovy instead, the dowside is that execution will take a lot longer.

Known issues

The current approach parses the xml before it formats it which means a parse fail will result in an error. Let me know if this is an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment