Convert XML to CSV conversion different option:
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.util.*
import groovy.xml.XmlUtil;
import groovy.util.XmlSlurper;
import groovy.xml.MarkupBuilder;
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def det
def body1
def lines = body.split("\n")
def CAMPAIGN = lines[1].trim()
def POLY = lines[3].trim()
def BATCH = lines[5].trim()
def UOM = lines[7].trim()
def PRODUCT = lines[9].trim()
def LINE = lines[11].trim()
def VERSION = lines[13].trim()
def TIMESTAMP = lines[15].trim()
def ABORTED = lines[17].trim()
for(i=19;i<=lines.size();i++)
{
if(lines[i-1].trim() != "EVENTS:"){
det = i;
}
else{ break;}
}
for(j=19;j<det;j++)
{
def data = lines[j].trim()
data = data.split(/\s+/)
body1 += "\n<CONS>\n";
body1 += "<Name>${data[0]}</Name>\n";
body1 += "<value>${data[1]}</value>\n";
body1 += "<Test>${data[2]}</Test>\n";
body1 += "</CONS>";
}
println body1
//def xmlBuilder = new StringBuilder()
//xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
def xmlString = """
<data>
<CAMPAIGN>${CAMPAIGN}</CAMPAIGN>
<POLY>${POLY}</POLY>
<BATCH>${BATCH}</BATCH>
<UOM>${POLY}</UOM>
<PRODUCT>${BATCH}</PRODUCT>
<LINE>${LINE}</LINE>
<VERSION>${VERSION}</VERSION>
<TIMESTAMP>${TIMESTAMP}</TIMESTAMP>
<ABORTED>${ABORTED}</ABORTED>
<CONSUMPTIONS>${body1}</CONSUMPTIONS>
</data>
"""
xmlString = xmlString.replaceAll("null","")
message.setProperty("data",xmlString)
return message
}
No comments:
Post a Comment