Convert generic csv to xml in Go

huangapple go评论142阅读模式
英文:

Convert generic csv to xml in Go

问题

我正在尝试将通用的CSV文件转换为XML文件。CSV文件有一行标题。标题值表示元素名称,相应列中的值是相应的元素值。

到目前为止,我的方法是:

// 读取CSV文件
file, err := os.Open(*i)
if err != nil {
log.Fatalf("打开输入文件时出错:%v\n", err)
}
defer file.Close()

r := csv.NewReader(file)
r.Comma, _ = utf8.DecodeRuneInString(*s)
lines, err := r.ReadAll()

// 标题值
header := lines[0]
// 写入XML文件
fileOut, err := os.Create(*o)
if err != nil {
log.Fatalf("打开输出文件时出错:%v\n", err)
}
defer fileOut.Close()

for _, l := range lines {
for i, elem := range l {
// TODO: 这里缺少一些内容...
xml.EscapeText(fileOut, []byte(header[i]))

xml.EscapeText(fileOut, []byte(elem))

}

问题:如何使XML写入部分的最后一部分工作?或者有没有更高效的方法将CSV文件转换为XML文件?

英文:

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\n", 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\n", 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?

答案1

得分: 1

有两种解决方案:

  1. 如果你能获取到结构体、字段和标签,你可以将数据反射到一个结构体中,然后使用xml包中的方法输出XML。
  2. 但是,如果像我所面临的情况一样,你不知道结构体的具体信息,有一种愚蠢但简单的方法,就是将数据以字符串的形式写入,如下所示:
data := XML_HEADER
data = data + "<records>\n"
for i := 2; i < len(records); i++ {
    data = data + "  <record "
    for j := 0; j < len(records[i]); j++ {
        data = data + head[j] + `="` + records[i][j] + `" `
    }
    data = data + "/>\n"
}
data = data + "</records>"
ioutil.WriteFile(xmlFileName, []byte(data), 0644)

以上是翻译好的内容,请确认是否满意。

英文:

The are two solutions:
You can reflect the data into a struct,only when you can get the struct , fields and tags. then output XML by methods from xml package.
But,if you don't know the struct as I face.there is a stupid but easy way,writing as string, as blow:

data := XML_HEADER
data = data + &quot;&lt;records&gt;\n&quot;
for i := 2; i &lt; len(records); i++ {
 data = data + &quot;  &lt;record &quot;
  for j := 0; j &lt; len(records[i]); j++  {
   data = data + head[j] + `=&quot;` + records[i][j] + `&quot; `
                                        }
 data = data + &quot;/&gt;\n&quot;              }
 data = data + &quot;&lt;/records&gt;&quot;
 ioutil.WriteFile(xmlFileName, []byte(data), 0644)

答案2

得分: 0

Go语言内置了对CSV和XML的读写支持。

请查看以下包:

http://golang.org/pkg/encoding/csv/

http://golang.org/pkg/encoding/xml/

将CSV数据读入结构体,然后将XML输出到文件应该没有问题。

英文:

Go has built in support for reading and writing CSV and XML.

Checkout the following packages:

http://golang.org/pkg/encoding/csv/

http://golang.org/pkg/encoding/xml/

It should be no problem to read your CSV data into a struct and then output XML to a file

huangapple
  • 本文由 发表于 2014年1月17日 19:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/21184333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定