Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const csv = require('fast-csv');
const xml = require("js2xmlparser");
const fs = require('fs');
const doc = [];
const options = {
declaration:{
include: false
}
}
const stream = fs.createReadStream("input.csv");
csv
.fromStream(stream, {delimiter : ';', headers: true})
.on("data", function(data){
let name = data.name;
let content = data.content;
let review_url = data.review_url;
let rating = data.rating;
let product_url = data.product_url;
let review_timestamp = data.review_timestamp;
let obj = {
"reviewer": {
"name": name
},
"review_timestamp": review_timestamp,
"content": content,
"review_url": {
"@": {
"type": "singleton"
},
"#": review_url
},
"ratings": {
"overall": {
"@": {
"min": 1,
"max": 5
},
"#": rating
}
},
"products": {
"product": {
"product_url": product_url;
}
}
};
doc.push(obj);
})
.on("end", function(){
doc.forEach(function(row){
fs.appendFile('output.xml', xml.parse("review", row, options) + '\n', 'utf-8', (err) => {
if (err) throw err;
});
});
fs.appendFile('output.xml', "</reviews></feed>", (err) => {
if(err) throw err;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.