我被迫使用一些设计不佳的 XML,我正在尝试将这个 XML 读入 Go 结构。以下是一些示例数据:
<?xml version="1.0" encoding="UTF-8"?>
<dictionary>
<direction from="lojban" to="English">
<valsi word="cipni" type="gismo">
<rafsi>cpi</rafsi>
<definition>x1 is a bird of species x2</definition>
<notes></notes>
</valsi>
...
</direction>
<direction from="English" to="lojban">
<nlword word="eagle" valsi="atkuila" />
<nlword word="hawk" sense="bird" valsi="aksiptrina" />
...
</direction>
</dictionary>
我的问题是我可以读取节点,或者因为它们都包含属性“word”:
main.NLWord field "Word" with tag "word,attr" conflicts with field "Valsi" with tag "word,attr"
我开始认为解组可能是错误的方法,因为理想情况下我会以不同的方式构造数据。我应该使用其他方法读取 XML 并手动构建数据结构吗?
type Valsi struct {
Word string `xml:"word,attr"`
Type string `xml:"type,attr"`
Def string `xml:"definition"`
Notes string `xml:"notes"`
Class string `xml:"selmaho"`
Rafsi []string `xml:"rafsi"`
}
//Whoever made this XML structure needs to be painfully taught a lesson...
type Collection struct {
From string `xml:"from"`
To string `xml:"to"`
Valsi []Valsi `xml:"valsi"`
}
type Vlaste struct {
Direction []Collection `xml:"direction"`
}
var httpc = &http.Client{}
func parseXML(data []byte) Vlaste {
vlaste := Vlaste{}
err := xml.Unmarshal(data, &vlaste)
if err != nil {
fmt.Println("Problem Decoding!")
log.Fatal(err)
}
return vlaste
}