-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabularizeXml
More file actions
executable file
·45 lines (40 loc) · 1.21 KB
/
tabularizeXml
File metadata and controls
executable file
·45 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env groovy
/**
* tabularizeXml
* @author Kevin Dorff
*
* This tool is designed to take tabular XML export from TM SmartGrids and convert it
* to CSV for easier comparison.
*
* The input file should be
* * XML
* * Tabular in nature, it's expected that each "<row>" should contain the same fields in the same order.
*
* You can use the following command to quickly view the data in the XML file as a table:
* $ tabularizeXml export-file.xml | column -t -s, | less -S
*/
if (!args || args.size() != 1) {
System.err.println "Please provide a single argument to a tabular file with a root node of <row>".
System.exit(-1)
}
def rows = new XmlSlurper().parseText(new File(args[0]).text)
allRows = []
List allFields = ['rownum']
rows.row.eachWithIndex { row, rowIndex ->
List currentRow = []
row.children().eachWithIndex { tag, colIndex ->
if (rowIndex == 0) {
allFields << tag.name().trim()
// println "Observing column '${tag.name().trim()}'"
}
if (colIndex == 0) {
currentRow << rowIndex
}
currentRow << "'${tag.text().trim()}'"
}
allRows << (currentRow.join(', '))
}
println allFields.join(', ')
allRows.eachWithIndex { row, rowIndex ->
println "${row}"
}