I am trying to convert generic csv files into xml files. The csv file has a header line. The header values represent the element name, and the values in the respective column are the corresponding element values.
My approach so far:
// Read the csv file
file, err := os.Open(*i)
if err != nil {
log.Fatalf("Error opening input file: %v
", err)
}
defer file.Close()
r := csv.NewReader(file)
r.Comma, _ = utf8.DecodeRuneInString(*s)
lines, err := r.ReadAll()
// header values
header := lines[0]
// Write the xml file
fileOut, err := os.Create(*o)
if err != nil {
log.Fatalf("Error opening input file: %v
", err)
}
defer fileOut.Close()
for _, l := range lines {
for i, elem := range l {
// TODO: Something here is missing...
xml.EscapeText(fileOut, []byte(header[i]))
xml.EscapeText(fileOut, []byte(elem))
}
Question: How do I make it the last part in xml write section work? Or is there a more efficient way of converting csv files into xml files?