Skip to content

Instantly share code, notes, and snippets.

@jlgeering
Created January 16, 2012 17:30
Show Gist options
  • Save jlgeering/1621924 to your computer and use it in GitHub Desktop.
Save jlgeering/1621924 to your computer and use it in GitHub Desktop.
xml indent script
#!/usr/bin/env groovy
def indent(args) {
def cli = new CliBuilder(usage: 'indent.groovy -[h] [-o outputFile] inputFile')
cli.with {
h longOpt: 'help', 'Show usage information'
o longOpt: 'out', args: 1, argName: 'outputFile', 'file to write to (default: stdout)'
}
def options = cli.parse(args)
if (!options) {
return
}
if (options.h || !options.arguments()) {
cli.usage()
return
}
if (options.o) {
new File(options.o).withPrintWriter { out ->
indent(options.arguments(), out)
}
}
else {
def autoFlush = true
indent(options.arguments(), new PrintWriter(System.out, autoFlush))
}
}
def indent(List inputFiles, PrintWriter out) {
inputFiles.each { inputFile ->
def node = new XmlParser().parse(inputFile)
new XmlNodePrinter(out).print(node)
}
}
indent(args);
#!/bin/bash
# from http://mrhaki.blogspot.com/2009/09/groovy-goodness-parsing-commandline.html
# usage: indent.sh file1.xml file2.xml file3.xml
xsltproc - $@ <<EOF
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment