on 2023 May 19 2:54 PM
Hi,
I have a script that will read the content of a CSV file line by line and do an operation for each line.
However the interface is taking a long time at this script step, it takes 24mins to finish processing a file with 30k lines of content:
So it actually causing a bottleneck in the whole process. This is the groovy script:
def body = message.getBody(java.io.Reader);
def xmlBody = ""
// header
xmlBody = '<?xml version="1.0" encoding="utf-8"?><ns:root>'
// loop the file content line by line
body.eachLine { line ->
xmlBody += '<Records>'
String[] data = line.split(',')
// set each field
xmlBody += '<field1>' + data[0].trim() + '</field1>'
xmlBody += '<field2>' + data[1].trim() + '</field2>'
xmlBody += '<field3>' + data[2].trim() + '</field3>'
xmlBody += '</Records>'
}
}
xmlBody += '</ns:root>'
message.setBody(xmlBody);
Is there any other way to read a file content line by line in a faster way?
EDIT: Reason why I don't use the pre-defined CSV to XML converter is because it's not a simple conversion and additional logic is required (splitting 1 csv field to 2 xml elements)
Thank you.
Request clarification before answering.
Hello,
Could you check the following code please?
def Message processData(Message message) {
Reader reader = message.getBody(java.io.Reader)
StringBuilder sb = new StringBuilder('<?xml version="1.0" encoding="utf-8"?><ns:root>')
reader.eachLine { line ->
String[] data = line.split(',')
sb.append("<Records><field1>${data[0].trim()}</field1>")
sb.append("<field2>${data[1].trim()}</field2>")
sb.append("<field3>${data[2].trim()}</field3></Records>")
}
sb.append('</ns:root>')
message.setBody(sb.toString())
return message
}
Regards,
Andrzej
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
suwandi.cahyadi
try it if this could work faster in loop
// loop the file content line by line
body.eachLine { line ->
xmlBody += '<Records>'
String[] data = line.split(',')
// set each field dynamically using loop
for (int i=0; i<data.length; i++) {
xmlBody += '<field' + (i+1) + '>' + data[i].trim() + '</field' + (i+1) + '>'
}
xmlBody += '</Records>'
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
73 | |
18 | |
10 | |
9 | |
7 | |
4 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.