Csv to xml with various tag name



I have this csv file



name;num_tel;num_fixe;id_client;num_comd;email;city;date_liv
gwenael;0998452223;1038431234;50C;12345;gwa@yahoo.fr;London;08/07/2015
marcel;0966442312;1038453211;31C;654321;marcel@yahoo.fr;Pairs;08/06/2015
judith;0954674487;1045227937;23D;78965;judith@yahoo.fr;Toulouse;11/05/2015
paul;0998452223;1038431234;35X;19945;paul@yahoo.fr;Bordeaux;01/04/2015
toto;0966442312;1038453211;31Z;994991;toto@yahoo.frNice;02/12/2015
marie;0954674487;1045227937;23C;78944;marie@yahoo.fr;Lille;04/08/2015
jacque;0998452223;1038431234;77C;18845;jacque@yahoo.fr;Bruges;09/05/2015
trucmuche;0966442312;1038453211;31Z;666321;trucmuche@yahoo.fr;Berlin;10/04/2015
tata;0954674487;1045227937;23D;77965;tata@yahoo.fr;New-york;08/07/2015


In my case, those tag name are in default value, users have the possibility to had more tag name for exemple i added city, date_liv. So they add it and generate the csv. they appear after the last default tag name (email). i would like to know if it is possible to make a loop to check into the csv and add them to the xml ? I think the loop will looks like for(i=7; i<=NF;i++) {} ? but how to make it ? Here is what i did can you help me to well formed it please i have so much mistake



BEGIN {

FS=";"
documentEnclosingTag = "rows"
c_flds["id_client"];c_flds["name"];c_flds["num_cmd"];c_flds["num_tel"];d_flds["email"]
d_flds["id_client"];d_flds["name"];d_flds["num_fixe"];d_flds["num_tel"];

print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
printf "<%s>\n", documentEnclosingTag
}

FNR==1 { gsub(" ", ""); for(i=1; i<=NF; i++) cols[$i]=i; next }

var = $(cols["ID_client_proxy"])
if ( var ~ /C/ )
{ print createObject( "C", c_flds ) }
else { print createObject( "D", d_flds ) }

END { printf "</%s>\n", documentEnclosingTag }

#----------- functions -----------

function createObject( enclosingTag, flds,key, s) {
for(key in flds) {
s = s "\t" wrapData( key, $(cols[key]) ) "\n"
}
return( wrapData( enclosingTag, "\n" s ) )
}

function wrapData( enclosingTag, data ) {
return( sprintf( "<%s>%s</%s>", enclosingTag, data, enclosingTag ) )
}

No comments:

Post a Comment