2

i am new in javascript i need to convert csv string data into xml look like below

CSV data:

   product,pakageing,qty
   mno,100ML,200
   pqr,400ML,300
   abc,150ML,100   

XML should be as below:

 <productData>
   <product>mno</product>
   <pakageing>100ML</pakageing
   <qty>200</qty>
 </productData>
 <productData>
   <product>pqr</product>
   <pakageing>400ML</pakageing
   <qty>300</qty>
 </productData>
 <productData>
   <product>abc</product>
   <pakageing>150ML</pakageing
   <qty>100</qty>
 </productData>
1
  • first you need to convert it into object and then convert it into xml Commented Dec 22, 2017 at 10:57

2 Answers 2

3

Try this.

Split the CSV data by new line, get the heading tags from the first line and iterate over the rest of the data and keep putting the data into the XML string through a loop.

let csvData = `product,pakageing,qty
   mno,100ML,200
   pqr,400ML,300
   abc,150ML,100`;

csvData = csvData.split('\n').map(row => row.trim())

let headings = csvData[0].split(',')

let xml = ``

for(let i = 1; i < csvData.length; i++) {
  let details = csvData[i].split(',')
  xml += "<productData>\n"
  for(let j = 0; j < headings.length; j++) {
    xml += `<${headings[j]}>${details[j]}</${headings[j]}>
    `;
  }
  xml += "</productData>\n"
}

console.log(xml);

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

1 Comment

Please add some explanation on what the code does, and how. Simply pasting code as an answer may help the OP, but does not provide much help for other users with similar problems
0

Try this

Common Function

function convertToXML(csvData) {
    var dataArr = csvData.split("\n");
    var heading = dataArr[0].split(",");
    var data = dataArr.splice(1, dataArr.length - 1);
    var xmlData = document.createElement("XmlData");
    for (var i = 0; i < data.length; i++) {
        var d = data[i].split(",");
        var productData = document.createElement("productData");
        for (var j = 0; j < d.length; j++) {
            var tag = document.createElement(heading[j]);
            tag.innerHTML = d[j];
            productData.appendChild(tag)
        }
        xmlData.appendChild(productData);
    }
    return xmlData.innerHTML;
}

Example:

//Adding default data to textbox
$("#tbCSV").val("product,pakageing,qty\n\
mno,100ML,200\n\
pqr,400ML,300\n\
abc,150ML,100");

//Binding Click Method
$("#btnConvert").click(function(){
  var xmlData=convertToXML($("#tbCSV").val());
  $("#tbXML").html(xmlData);
});

//Function which is used to convert CSV data to XML
function convertToXML(csvData){
  var dataArr=csvData.split("\n");
var heading=dataArr[0].split(",");
var data=dataArr.splice(1,dataArr.length-1);
var xmlData=document.createElement("XmlData");
for(var i=0;i<data.length;i++){
  var d=data[i].split(",");
  var productData=document.createElement("productData");
  for(var j=0;j<d.length;j++){
    var tag=document.createElement(heading[j]);
    tag.innerHTML=d[j];
    productData.appendChild(tag)
  }
  xmlData.appendChild(productData);
}
  return xmlData.innerHTML;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="" id="tbCSV" cols="35" rows="5"></textarea>
<textarea name="" id="tbXML" cols="35" rows="5"></textarea>
<br/>
<button id="btnConvert">Convert To XML</button>

Here is the link https://codepen.io/anon/pen/QaKxKK

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.