Last active
June 3, 2020 10:39
-
-
Save EvgenJin/48cdf72d06a1981cee257b5ac3990aa5 to your computer and use it in GitHub Desktop.
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
def headerRow | |
def rowList = new ArrayList<ReportRow>(); | |
def dataXml = new XmlSlurper().parse("./employee.xml"); | |
def sb = new StringBuffer(); | |
def getKeyValue(obj, type) { | |
def String res | |
if (type == "key") { | |
res = obj.getProperties().findAll{name,value -> name != 'class'}.sort{(it.key.toString() =~ /\d+/)[0].toInteger()}.collect{it.key}.join(',') | |
} | |
else if (type == "value") { | |
res = obj.getProperties().findAll{name,value -> name != 'class'}.sort{(it.key.toString() =~ /\d+/)[0].toInteger()}.collect{it.value}.join(',') | |
} | |
} | |
headerRow = getKeyValue(new ReportRow(),"key") | |
dataXml.employee.each{ node -> | |
def main_row = new ReportRow(); | |
main_row.column_1 = node.'@id' | |
main_row.column_2 = node.firstName | |
main_row.column_3 = node.lastName | |
main_row.column_4 = node.location | |
main_rowCsv = getKeyValue(main_row,"value") | |
sb.append(main_rowCsv).append('\n') | |
node.events.event.each{ | |
def event = new ReportRow(); | |
event.column_1 = node.'@id' | |
event.column_2 = node.firstName | |
event.column_3 = node.lastName | |
event.column_4 = node.location | |
event.column_5 = it.'@id' | |
event.column_6 = it.'@datein' | |
event.column_7 = it.'@dateout' | |
event_rowCsv = getKeyValue(event,"value") | |
sb.append(event_rowCsv).append('\n') | |
} | |
} | |
def file = new File("./test.csv") | |
if (!file.exists()) { | |
file.write(headerRow); | |
file.append('\n'); | |
file.append(sb.toString()) | |
println "csv file not exists -> print header" | |
} | |
else { | |
file.append(sb.toString()) | |
println "csv file already exists" | |
} | |
class ReportRow { | |
String column_1 = '' | |
String column_2 = '' | |
String column_3 = '' | |
String column_4 = '' | |
String column_5 = '' | |
String column_6 = '' | |
String column_7 = '' | |
String column_8 = '' | |
String column_9 = '' | |
String column_10 = '' | |
String column_11 = '' | |
String column_12 = '' | |
String column_13 = '' | |
String column_14 = '' | |
String column_15 = '' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment