The file I have to transform is an automatically generated XML, like this:
<_pdl></_pdl> <_tkn></_tkn> <_out></_out> <_tds></_tds> <_evt></_evt> <_lfn></_lfn> <tc></tc> <fc></fc>
I want to strip things out to get only the start elements, like this:
<_pdl> <_tkn> <_out> <_tds> <_evt> <_lfn> <tc> <fc>
A quick and dirty Groovy solution can be something like:
def file = "C:\\Users\\mauri\\Desktop\\mapping.txt"
StringBuffer dest = new StringBuffer();
new File(file).eachLine{ line -> line = line.trim()
if (line.equals("")) {}
else if (line.indexOf("/") > -1) {
dest.append(line.substring(0, line.indexOf("/")-1))
dest.append("\n")
} else {
dest.append(line)
dest.append("\n")
}
}
print dest.toString()
Probably there are much better ways to do that in Groovy (I'm a rookie here), but I can recycle most of my Java skills and well known Java syntax, which saves me a lot of time. Especially, even if I have to learn new interesting things like closures, I can re-use years of knowledge about the Java API, which from my point of view makes Groovy a winning tool in the JVM world.
