在Go中解组XML

时间:2015-08-20 18:34:55

标签: xml go

我有以下xml“文件”:

<pinnacle_line_feed>
    <PinnacleFeedTime>1439954818555</PinnacleFeedTime>
    <lastContest>34317132</lastContest>
    <lastGame>218491030</lastGame>
    <events>
        <event>
            <event_datetimeGMT>2015-08-21 09:50</event_datetimeGMT>
            <gamenumber>483406220</gamenumber>
            <sporttype>Aussie Rules</sporttype>
            <league>AFL</league>
            <IsLive>No</IsLive>
            <participants>
                <participant>
                    <participant_name>Hawthorn Hawks</participant_name>
                    <contestantnum>1251</contestantnum>
                    <rotnum>1251</rotnum>
                    <visiting_home_draw>Visiting</visiting_home_draw>
                    </participant>
                <participant>
                    <participant_name>Port Adelaide Power</participant_name>
                    <contestantnum>1252</contestantnum>
                    <rotnum>1252</rotnum>
                    <visiting_home_draw>Home</visiting_home_draw>
                </participant>
            </participants>
            <periods></periods>
        </event>
    </events>
</pinnacle_line_feed>

我试图用Golang解析它,到目前为止写了下面的内容:

package main
import (
	"fmt"
	"encoding/xml"
)

type Participant struct {
	XMLName            xml.Name `xml:"participant"`
	participant_name   string `xml:"participant_name"`
	contestantnum      int `xml:"contestantnum"`
	rotnum             int `xml:"rotnum"`
	visiting_home_draw string `xml:"visiting_home_draw"`
}

type Event struct {
	XMLName           xml.Name `xml:"event"`
	event_datetimeGMT string `xml:"event_datetimeGMT"`
	gamenumber        string `xml:"gamenumber"`
	sporttype         string `xml:"sporttype"`
	league            string `xml:"league"`
	IsLive            string `xml:"IsLive"`
	Participant       []Participant `xml:"participant"`
}

type Events struct {
	XMLName xml.Name `xml:"events"`
	Event   []Event `xml:"event"`
}

type Pinnacle_Line_Feed struct {
	XMLName          xml.Name `xml:"pinnacle_line_feed"`
	PinnacleFeedTime string `xml:"PinnacleFeedTime"`
	lastContest      string `xml:"lastContest"`
	lastGame         string `xml:"lastGame"`
	Events           []Events `xml:"events"`
}

坦率地说,我已经对Golang中的深层嵌套xml解析进行了大量研究,而且我没有找到太多帮助解决此代码的Unmarshal部分。理想情况下,代码将返回类似于python字典的内容,例如

{event_datetimeGMT:2015-08-21,gamenumber:483406220,...,visiting_home_draw:“Home”}

修改

根据Nicolas在下面的评论,我添加了以下内容。这样运行没有错误,但产生一个空的结果。

 func main() {
         pinny_xml := `
         <pinnacle_line_feed>
            <PinnacleFeedTime>1439954818555</PinnacleFeedTime>
            <lastContest>34317132</lastContest>
            <lastGame>218491030</lastGame>
            <events>
                <event>
                    <event_datetimeGMT>2015-08-21 09:50</event_datetimeGMT>
                    <gamenumber>483406220</gamenumber>
                    <sporttype>Aussie Rules</sporttype>
                    <league>AFL</league>
                    <IsLive>No</IsLive>
                    <participants>
                        <participant>
                            <participant_name>Hawthorn Hawks</participant_name>
                            <contestantnum>1251</contestantnum>
                            <rotnum>1251</rotnum>
                            <visiting_home_draw>Visiting</visiting_home_draw>
                            </participant>
                        <participant>
                            <participant_name>Port Adelaide Power</participant_name>
                            <contestantnum>1252</contestantnum>
                            <rotnum>1252</rotnum>
                            <visiting_home_draw>Home</visiting_home_draw>
                        </participant>
                    </participants>
                    <periods></periods>
                </event>
            </events>
        </pinnacle_line_feed>
        `

        xmlReader := bytes.NewReader([]byte(pinny_xml))
        yourPinnacleLineFeed := new(Pinnacle_Line_Feed)
        if err := xml.NewDecoder(xmlReader).Decode(yourPinnacleLineFeed); err != nil {
            return // or log.Panic(err.Error()) if in main
        }
}

2 个答案:

答案 0 :(得分:2)

你可以这样做:

xmlReader := bytes.NewReader([]byte(your_xml_as_a_string_here))
yourPinnacleLineFeed := new(Pinnacle_Line_Feed)
if err := xml.NewDecoder(xmlReader).Decode(yourPinnacleLineFeed); err != nil {
    return // or log.Panic(err.Error()) if in main
}

这是你的xml&#34;文件&#34;是一个字符串。如果您从互联网上获取它(例如,作为http响应的正文),resp.Body已经满足了Reader,因此您可以跳过第一行。如果您在操作系统上打开一个真实文件,您也可以将其作为读者打开,同样的事情。

编辑:还有两件事:

  • 为简单明了,您可以嵌套结构并删除xml.Name字段
  • 我也注意到你忘记了结构中的参与者级别,导致没有参与者被解组

这是一个更简单的版本,有一个可选功能来检查结果中的内容:

package main

import (
    "bytes"
    "encoding/json"
    "encoding/xml"
    "fmt"
    "log"
)

type Pinnacle_Line_Feed struct {
    PinnacleFeedTime string `xml:"PinnacleFeedTime"`
    LastContest      string `xml:"lastContest"`
    LastGame         string `xml:"lastGame"`
    Events           struct {
        Event []struct {
            Event_datetimeGMT string `xml:"event_datetimeGMT"`
            Gamenumber        string `xml:"gamenumber"`
            Sporttype         string `xml:"sporttype"`
            League            string `xml:"league"`
            IsLive            string `xml:"IsLive"`
            Participants      struct {
                Participant []struct {
                    Participant_name   string `xml:"participant_name"`
                    Contestantnum      int    `xml:"contestantnum"`
                    Rotnum             int    `xml:"rotnum"`
                    Visiting_home_draw string `xml:"visiting_home_draw"`
                } `xml:"participant"`
            } `xml:"participants"`
        } `xml:"event"`
    } `xml:"events"`
}

func main() {
    pinny_xml := `
         <pinnacle_line_feed>
            <PinnacleFeedTime>1439954818555</PinnacleFeedTime>
            <lastContest>34317132</lastContest>
            <lastGame>218491030</lastGame>
            <events>
                <event>
                    <event_datetimeGMT>2015-08-21 09:50</event_datetimeGMT>
                    <gamenumber>483406220</gamenumber>
                    <sporttype>Aussie Rules</sporttype>
                    <league>AFL</league>
                    <IsLive>No</IsLive>
                    <participants>
                        <participant>
                            <participant_name>Hawthorn Hawks</participant_name>
                            <contestantnum>1251</contestantnum>
                            <rotnum>1251</rotnum>
                            <visiting_home_draw>Visiting</visiting_home_draw>
                        </participant>
                        <participant>
                            <participant_name>Port Adelaide Power</participant_name>
                            <contestantnum>1252</contestantnum>
                            <rotnum>1252</rotnum>
                            <visiting_home_draw>Home</visiting_home_draw>
                        </participant>
                    </participants>
                    <periods></periods>
                </event>
            </events>
        </pinnacle_line_feed>
    `

    xmlReader := bytes.NewReader([]byte(pinny_xml))
    yourPinnacleLineFeed := new(Pinnacle_Line_Feed)
    if err := xml.NewDecoder(xmlReader).Decode(yourPinnacleLineFeed); err != nil {
        log.Panic(err.Error())
    }

    printX(yourPinnacleLineFeed)
}

func printX(x interface{}) (err error) {
    var xBytes []byte
    xBytes, err = json.MarshalIndent(x, "", "  ")
    if err != nil {
        return
    }
    fmt.Println(string(xBytes))
    return
}

答案 1 :(得分:1)

您需要对结构中的所有字段进行大写。 xml解码器需要导出字段才能正常工作。去游乐场here