Created
January 18, 2011 16:13
-
-
Save xlson/784665 to your computer and use it in GitHub Desktop.
Pretty-prints XML on the commandline (requires groovyserv)
This file contains hidden or 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 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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.