I need to write my JavaScript function that will iterate over an input type file to get the content of multiple XML files (one by one) and concatenate it in such a way that first line of content in all files (except first file) is removed and rest content is in one single variable or JavaScript array or whatever works with browser. I have spent days researching all over Google but could not get it to work so, finally posting here.
function call(){
var myFile = $('#xmlFileUploaded').get(0);
var xml=new Array();
for (let i = 0; i < myFile.files.length; i++) {
(function(file){
let reader = new FileReader();
reader.onload = function() {
xml.push(reader.result);
}
reader.readAsText(file);
})(myFile.files[i]);
}
console.log(xml);
for(let x=0;x<xml.length;x++)
console.log(xml[x]);
}
After spending hours and days on this I have created above code. I run it and select 3 XML files. console.log in Chrome is showing empty array []. When I click on the little triangle to see details it shows correct array length i.e. 3 and content of the 3 files. Now starts the confusing statement, if I iterate over this array it gives me nothing.
Sample XML files:
one.xml:
<myroot>
<tag1>value1</tag1>
<tag2>value2</tag2>
</myroot>
two.xml
<myroot>
<tag1>value3</tag1>
<tag2>value4</tag2>
</myroot>
three.xml
<myroot>
<tag1>value5</tag1>
<tag2>value6</tag2>
</myroot>
Desired Output:
<myroot>
<tag1>value1</tag1>
<tag2>value2</tag2>
<tag1>value3</tag1>
<tag2>value4</tag2>
<tag1>value5</tag1>
<tag2>value6</tag2>
</myroot>