无法使用golang解码XML,始终得到空结构体。

huangapple go评论87阅读模式
英文:

Can't decode XML with golang, always empty struct

问题

我正在尝试使用golang解码XML,但以下代码返回一个空的结构体。

有人可以帮忙吗?

当我运行以下代码时,总是得到:

{{ packet} []}

附上源代码:

package main

import (
    "fmt"
    "encoding/xml"
//    "io/ioutil"
)

type Field struct {
    XMLName xml.Name `xml:"field"`
    name      string `xml:"name,attr"`
    shownameg string `xml:"showname,attr"`
    fields []Field
}

type Proto struct {
    XMLName xml.Name `xml:"proto"`
    name      string `xml:"name,attr"`
    shownameg string `xml:"showname,attr"`
    fields []Field
}

type Packet struct {
    XMLName xml.Name `xml:"packet"`
    protos []Proto   `xml:"proto"`
}

func main () {   
    data := []byte(`
<packet>
  <proto name="geninfo" pos="0" showname="General information" size="122">
    <field name="timestamp" pos="0" show="Jul 17, 2008 15:50:25.136434000 CST" showname="Captured Time" value="1216281025.136434000" size="122"/>
  </proto>
</packet>
    `)

    packet := Packet{}

    err := xml.Unmarshal([]byte(data), &packet)
    if err != nil {
        fmt.Println (err)
        return
    }

    fmt.Println (packet)

    for proto, _ := range packet.protos {
        fmt.Println (proto)
    }
}
英文:

I'm trying to decode XML with golang, but the following code gives an empty struct

Anyone can help?

When I run the following code, I always get

{{ packet} []}

Attached source code:

package main

import (
    &quot;fmt&quot;
    &quot;encoding/xml&quot;
//    &quot;io/ioutil&quot;
)

type Field struct {
    XMLName xml.Name `xml:&quot;field&quot;`
    name      string `xml:&quot;name,attr&quot;`
    shownameg string `xml:&quot;showname,attr&quot;`
    fields []Field
}

type Proto struct {
    XMLName xml.Name `xml:&quot;proto&quot;`
    name      string `xml:&quot;name,attr&quot;`
    shownameg string `xml:&quot;showname,attr&quot;`
    fields []Field
}

type Packet struct {
    XMLName xml.Name `xml:&quot;packet&quot;`
    protos []Proto   `xml:&quot;proto&quot;`
}

func main () {   
    data := []byte(`
&lt;packet&gt;
  &lt;proto name=&quot;geninfo&quot; pos=&quot;0&quot; showname=&quot;General information&quot; size=&quot;122&quot;&gt;
    &lt;field name=&quot;timestamp&quot; pos=&quot;0&quot; show=&quot;Jul 17, 2008 15:50:25.136434000 CST&quot; showname=&quot;Captured Time&quot; value=&quot;1216281025.136434000&quot; size=&quot;122&quot;/&gt;
  &lt;/proto&gt;
&lt;/packet&gt;
    `)

    packet := Packet{}

    err := xml.Unmarshal([]byte(data), &amp;packet)
    if err != nil {
        fmt.Println (err)
        return
    }

    fmt.Println (packet)

    for proto, _ := range (packet.protos) {
        fmt.Println (proto)
    }
}

答案1

得分: 4

你需要按照 https://golang.org/pkg/encoding/xml/#Unmarshal 中的说明导出你的结构字段。

因为 Unmarshal 使用 reflect 包,它只能赋值给导出的(大写字母开头的)字段。Unmarshal 使用区分大小写的比较来将 XML 元素名称与标签值和结构字段名称进行匹配。

例如:

type Proto struct {
    XMLName   xml.Name `xml:"field"`
    Name      string   `xml:"name,attr"`
    Shownameg string   `xml:"showname,attr"`
    Fields    []Field
}
英文:

You need to export your struct fields as per https://golang.org/pkg/encoding/xml/#Unmarshal

> Because Unmarshal uses the reflect package, it can only assign to exported (upper case) fields. Unmarshal uses a case-sensitive comparison to match XML element names to tag values and struct field names.

e.g.

type Proto struct {
    XMLName xml.Name `xml:&quot;field&quot;`
    Name      string `xml:&quot;name,attr&quot;`
    Shownameg string `xml:&quot;showname,attr&quot;`
    Fields []Field
}

huangapple
  • 本文由 发表于 2016年1月31日 11:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/35110107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定