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.