dptx8888 2017-11-16 17:42
浏览 328
已采纳

将XML转换为CSV

I am new to go and trying to write a script to convert a SOAP response (currently stored as an xml file) and convert it to CSV.

Here is a copy of my xml

<?xml version="1.0" encoding="UTF-8" ?>
<Data>
    <Results>
        <Client>
            <ID>72rere341</ID>
        </Client>
        <PartnerKey xsi:nil="true" />
        <PartnerProperties>
            <Name>email</Name>
            <Value>example1@test1.com</Value>
        </PartnerProperties>
        <ID>755454475</ID>
        <ObjectID xsi:nil="true" />
        <UserID>5fd0acfc-6crerfrgrfe6e9a675f65</UserID>
        <ActiveFlag>true</ActiveFlag>
        <Delete>0</Delete>
        <LastSuccessfulLogin>2014-11-07T16:00:46.747</LastSuccessfulLogin>
    </Results>
    <Results>
        <Client>
            <ID>72rere5341</ID>
        </Client>
        <PartnerKey xsi:nil="true" />
        <PartnerProperties>
            <Name>email</Name>
            <Value>example2@test1.com</Value>
        </PartnerProperties>
        <ID>7225483</ID>
        <ObjectID xsi:nil="true" />
        <UserID>example2@test1.com</UserID>
        <ActiveFlag>false</ActiveFlag>
        <Delete>0</Delete>
        <LastSuccessfulLogin>2015-04-29T05:01:27.85</LastSuccessfulLogin>
    </Results>
</Data>

Here is a copy of my code

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "log"
    "os"


)

type AccountUser struct {
    UserID              string
    ActiveFlag          string
    LastSuccessfulLogin string
    //PartnerProperties   Partner `xml:"PartnerProperties"`
}

type Partner struct {
    Name  string
    Value string
}


type Query struct {
    ResultList []AccountUser `xml:"Results"`
}


func main() {
    xmlFile, err := os.Open("ActiveUsers.xml")
    if err != nil {
        fmt.Println("Error opening file:", err)

        return
    }
    defer xmlFile.Close()

    b, err := ioutil.ReadAll(xmlFile)
    if err != nil {
        log.Fatal(err)
    }


    var q Query
    err = xml.Unmarshal(b, &q)
    if err != nil {
        log.Fatal(err)
    }



     file, err := os.Create("result.csv")
    checkError("Cannot create file", err)
    defer file.Close()

    writer := csv.NewWriter(file)
    defer writer.Flush()

    for _, result := range q.ResultList  {
        err := writer.Write(result)
        checkError("Cannot write to file", err)
    }

}

func checkError(message string, err error) {
    if err != nil {
        log.Fatal(message, err)
    }


}

When I run this, I get this error:

./ActiveUsers.go:73:28: cannot use result (type AccountUser) as type []string in argument to writer.Write

I have a feel I might need to covert this to bytes or another data structure but unsure of how to do so.

I will appreciate any pointers on this.

  • 写回答

1条回答 默认 最新

  • duansai1314 2017-11-16 17:48
    关注

    csv.Writer writes rows to a CSV file, which it receives as a slice of strings ([]string). If you want to write a struct as a row in a CSV file, you must create a []string containing whatever fields you want included, converted to strings, in the order you want them to appear in columns in the CSV file, e.g.:

    for _, result := range q.ResultList  {
        err := writer.Write([]string{result.UserID, result.ActiveFlag, result.LastSuccessfulLogin})
        checkError("Cannot write to file", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效