dptx8888
dptx8888
2017-11-16 17:42
浏览 311

将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
    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)
    }
    
    点赞 评论

相关推荐