简体   繁体   中英

Converting XML file to CSV

I'm trying to parse an XML file to CSV. My code is running without giving any errors but result is empty. I can't figure out what is missing part. It looks like xml.unMarshal() doesn't working fine.

package domain

type Row struct {
    Id string `xml:"Id"`
    PostTypeId string `xml:"PostTypeId"`
    AcceptedAnswerId string `xml:"AcceptedAnswerId"`
    CreationDate string `xml:"CreationDate"`
    Score string `xml:"Score"`
    ViewCount string `xml:"ViewCount"`
    Body string `xml:"Body"`
    OwnerUserId string `xml:"OwnerUserId"`
    LastEditorUserId string `xml:"LastEditorUserId"`
    LastEditorDisplayName string `xml:"LastEditorDisplayName"`
    LastEditDate string `xml:"LastEditDate"`
    LastActivityDate string `xml:"LastActivityDate"`
    Title string `xml:"Title"`
    Tags string `xml:"Tags"`
    AnswerCount string `xml:"AnswerCount"`
    CommentCount string `xml:"CommentCount"`
    FavoriteCount string `xml:"FavoriteCount"`
    CommunityOwnedDate string `xml:"CommunityOwnedDate"`
}

type Posts struct {
    Posts []Row `xml:"row"`
}

package main

import (
    "encoding/csv"
    "encoding/xml"
    "fmt"
    "go-mod/xml-csv/domain"
    "io/ioutil"
    "log"
    "os"
)

func main()  {
    fmt.Println("start converting.....")
    xmlFile,err:=os.Open("Posts1.xml")
    if err!=nil{
        log.Fatalf("Error opening xml file fails:%s",err.Error())
    }
    defer func() {
        err=xmlFile.Close()
        if err!=nil{
            log.Printf("Error closing file:%s",err.Error())
        }
    }()
    b,err := ioutil.ReadAll(xmlFile)
    if err !=nil{
        log.Fatalf("Error reading file:%s",err.Error())
    }
    //fmt.Println(string(b))
    var doc domain.Posts

    err2:=xml.Unmarshal(b,&doc)
    if err2!=nil{
        fmt.Println("errrrrrrr")
        //log.Fatalf("Error unmarshaling to struct")
    }
    //fmt.Println(doc)
    file,err:=os.Create("result.csv")
    if err!=nil{
        log.Fatalf("Error creating csv file:%s",err.Error())
    }
    defer func() {
        err=file.Close()
        if err!=nil{
            log.Print("Error closing csv file")
        }
    }()
    writer := csv.NewWriter(file)
    defer writer.Flush()
    for _,result:=range doc.Posts{
        //fmt.Println(result)
        err:=writer.Write([]string{
            result.Id,
            result.AcceptedAnswerId,
            result.CreationDate,
            result.Score,
            result.ViewCount,
            result.Body,
            result.OwnerUserId,
            result.LastEditorUserId,
            result.LastEditorDisplayName,
            result.LastEditDate,
            result.LastActivityDate,
            result.Title,
            result.Tags,
            result.AnswerCount,
            result.CommentCount,
            result.FavoriteCount,
            result.CommunityOwnedDate,
        })
        if err!=nil{
            log.Fatalf("Error writing row to file:%s",err.Error())
        }
    }
}

For more undestanding I have put some sample xml document I have used in below

<?xml version="1.0" encoding="utf-8"?>
<posts>
  <row Id="4" PostTypeId="1" AcceptedAnswerId="7" CreationDate="2008-07-31T21:42:52.667" Score="604" ViewCount="39047" Body="p&gt;&#xA;" OwnerUserId="8" LastEditorUserId="6786713" LastEditorDisplayName="Rich B" LastEditDate="2018-07-02T17:55:27.247" LastActivityDate="2019-01-17T13:39:48.937" Title="Convert Decimal to Double?" Tags="&lt;c#&gt;&lt;floating-point&gt;&lt;type-conversion&gt;&lt;double&gt;&lt;decimal&gt;" AnswerCount="13" CommentCount="2" FavoriteCount="46" CommunityOwnedDate="2012-10-31T16:42:47.213" />
  <row Id="6" PostTypeId="1" AcceptedAnswerId="31" CreationDate="2008-07-31T22:08:08.620" Score="273" ViewCount="17307" Body="/ol&gt;&#xA;" OwnerUserId="9" LastEditorUserId="63550" LastEditorDisplayName="Rich B" LastEditDate="2016-03-19T06:05:48.487" LastActivityDate="2018-12-15T03:57:18.220" Title="Percentage width child element" Tags="&lt;html&gt;&lt;css&gt;&lt;css3&gt;&lt;internet-explorer-7&gt;" AnswerCount="6" CommentCount="0" FavoriteCount="11" />
</posts>

Your Row structure does not match xml itself. You have row element that contains attributes which means you need Id string `xml:"Id,attrs"` instead of Id string `xml:"Id"` and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM