将xml文件转换为csv golang

时间:2019-12-12 11:34:44

标签: xml csv go

我正在尝试将xml文件解析为csv。我的代码正在运行,没有给出任何错误,但结果为空。我不知道缺少什么。看起来xml.unMarshal()不能正常工作。任何帮助将不胜感激

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())
        }
    }
}

为了让大家更加了解,我在下面放置了一些我使用的示例xml文档。

<?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>

1 个答案:

答案 0 :(得分:4)

您的行结构与xml本身不匹配。您的row元素包含属性,这意味着您需要 Id string `xml:"Id,attrs"`代替Id string `xml:"Id"`,依此类推