0

I need to convert a csv file to xml in the following format:

The input csv is

scott,male,26,doctor
ryan,male,20,student
laura,female,30,lawyer

The resulting xml should be

<root>
<criteria name="scott" sex = "male" age = "26" profession = "doctor"/>
<criteria name="ryan" sex = "male" age = "20" profession = "student"/>
<criteria name="laura" sex = "female" age = "30" profession = "lawyer"/>
<root/>

and in the csv if any field is missing like profession,

laura,female,30

the resulting xml should be

<criteria name="laura" sex = "female" age = "30"/>

if the csv is :

 laura,female,,lawyer

it produces

<criteria name="laura" sex = "female" age = "" profession = "lawyer"/>

where as i want the xml to be

 <criteria name="laura" sex = "female" profession = "lawyer"/>

Can some one help me out with the code to do that in groovy?

The code i am using is

CommonsMultipartFile multiPartFile=fileList.first()
                params.type = 'text/csv'
                if(CSV_MIME_TYPE.contains(params.type)){
                    InputStream  inputStream= multiPartFile.getInputStream()
                    def writer=new StringWriter()
                    def xmlBuilder= new MarkupBuilder(writer)
                    xmlBuilder.setDoubleQuotes(true)
                    xmlBuilder.root(){  
                        inputStream.eachLine{ line->
                            def a,b,c,d,e,f,g
                            (a,b,c,d) = line.split(',',-1)
                                      criteria(name:a,sex:b,age:c,profession:d)
                        }

2 Answers 2

2

the easiest way to get rid of the empty fields there would be use another mapping approach. if the empty values are not in the map for the criteria() call, then nothing is generated there. so you could map and filter them like this:

def line = 'laura,female,,lawyer'
def mapping = ['name', 'sex', 'age', 'profession']
// use criteria instead of println there
println([mapping,line.split(',',-1)*.trim()].transpose().findAll{it[1]}.collectEntries())
//=> [name:laura, sex:female, profession:lawyer]

transpose pairs the keys from the mapping together with the values from the line (it only takes the same amount of each list, so missing data at the end of the line is no problem); next filter all emtpy values out and finally collectEntries turns those into a map

Sign up to request clarification or add additional context in comments.

Comments

0

Your code works as expected but there is an extra comma in the CSV snippet laura,female,,lawyer which is throwing off your CSV 'parser'.

The abbreviation of your code works as expected, here it is in Groovy web console (click 'execute script' and then open the 'output' tab - if the console loads up empty, try reloading it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.