最近,我遇到了将CSV转换为XML的挑战。 文件结构如下所示CSV file structure
CSV文件说明: -
CSV文件中的前3行描述了数据的类型。
这些行定义了节点。例如,等等。
我已经达到了预期的效果,但我还没有使用任何DOMPARSER或任何其他用于从CSV生成XML文件的预定义函数。
总的来说,我想知道的关键点是:
答案 0 :(得分:0)
function csvXML(csv){
var lines=csv.split(/\r?\n/);
var result = [];
var headerfile="";
var Linefile="";
var Linesplitfile="";
var mainfile="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ns0:MT_CoupaNoddles_Invoice_File xmlns:ns0=\"http://superearth.com/xi/p2p/coupanoddels_integration\">\n <Invoice>";
var HeaderPart=new Array();
var LinePart=new Array();
var LineSplitPart=new Array();
var type;
var endstring="\n </Invoice>\n</ns0:MT_Coupa_Invoice_File>";
var newLine= lines.length-1;
//Load all the 3 types in array Header Line And LineSplit
for(var i=0;i<3;i++)
{
if(i==0)
{
debugger;
var tempHeaderPart=lines[i].split(",");
for(var j=0;j<tempHeaderPart.length;j++)
{
if(tempHeaderPart[j]!="")
{
HeaderPart[j]=tempHeaderPart[j];
}
else{
debugger;
console.log("line number is: ",j,"The data is: ",tempHeaderPart[j]);
continue;
}
}
}
else if(i==1)
{
var tempLinePart=lines[i].split(",");
for(var j=0;j<tempLinePart.length;j++)
{
if(tempLinePart[j]!="")
{
LinePart[j]=tempLinePart[j];
}
else{
debugger;
console.log("line number is: ",j,"The data is: ",tempLinePart[j]);
continue;
}
}
}
else
{
var tempLineSplitPart=lines[i].split(",");
for(var j=0;j<tempLineSplitPart.length;j++)
{
if(tempLineSplitPart[j]!="")
{
LineSplitPart[j]=tempLineSplitPart[j];
}
else{
debugger;
console.log("line number is: ",j,"The data is: ",tempLineSplitPart[j]);
continue;
}
}
}
}//end for loop
for(var csvchar=3;csvchar<newLine;csvchar++)
{
debugger;
var proheaders=lines[csvchar].split(",");
//If loop for Header part
if(proheaders[0]=="Header")
{
for( var i=0;i<HeaderPart.length;i++)
{
if(i==0)
{
var temp="\n<"+proheaders[i]+">\n<"+HeaderPart[i]+">"+proheaders[i]+"</"+HeaderPart[i]+">";
headerfile=headerfile +temp;
continue;
}
else if(i==HeaderPart.length-1)
{
debugger;
var temp="\n<"+HeaderPart[i]+">"+proheaders[i]+"</"+HeaderPart[i]+">\n</"+proheaders[0]+">\n";
headerfile=headerfile +temp;
console.log(headerfile);
mainfile=mainfile + headerfile;
continue;
}
else
{
var temp="\n<"+HeaderPart[i]+">"+proheaders[i]+"</"+HeaderPart[i]+">";
headerfile=headerfile +temp;
}
}
}//end of If loop for Header part
//If loop for Line part
else if(proheaders[0]=="Line")
{
debugger;
for( var i=0;i<LinePart.length;i++)
{
if(i==0)
{
debugger;
var temp="<"+proheaders[i]+">\n<"+LinePart[i]+">"+proheaders[i]+"</"+LinePart[i]+">";
Linefile=Linefile +temp;
continue;
}
else if(i==LinePart.length-1)
{
debugger;
var temp="\n<"+LinePart[i]+">"+proheaders[i]+"</"+LinePart[i]+">\n</"+proheaders[0]+">\n";
Linefile=Linefile +temp;
console.log(Linefile);
mainfile=mainfile + Linefile;
continue;
}
else
{
var temp="\n<"+LinePart[i]+">"+proheaders[i]+"</"+LinePart[i]+">";
Linefile=Linefile +temp;
}
}
}
//End of If loop for Line part
//If loop for LineSplit part
else if(proheaders[0]=="Linesplit")
{
debugger;
for( var i=0;i<LineSplitPart.length;i++)
{
if(i==0)
{
debugger;
var temp="<"+proheaders[i]+">\n<"+LineSplitPart[i]+">"+proheaders[i]+"</"+LineSplitPart[i]+">";
Linesplitfile=Linesplitfile +temp;
continue;
}
else if(i==LineSplitPart.length-1)
{
debugger;
var temp="\n<"+LineSplitPart[i]+">"+proheaders[i]+"</"+LineSplitPart[i]+">\n</"+proheaders[0]+">\n";
Linesplitfile=Linesplitfile +temp;
console.log(Linesplitfile);
mainfile=mainfile + Linesplitfile;
continue;
}
else
{
var temp="\n<"+LineSplitPart[i]+">"+proheaders[i]+"</"+LineSplitPart[i]+">";
Linesplitfile=Linesplitfile +temp;
}
}
}
//End of If loop for Line split part
/*for(var j=0;j<proheaders.length;j++)
{
if(proheaders[j]!="")
{
var temp="\n<"+proheaders[j]+"/>";
mainfile=mainfile +temp;
console.log(mainfile);
}
else{
continue;
}
}*/
}
mainfile=mainfile +endstring;
console.log(mainfile);
return mainfile;
}
}
readCsv.readAsText(fileCsv);
}
else
{
alert("Failed to load file");
}
}