Golang XML解析
发布时间:2020-12-16 19:22:11 所属栏目:大数据 来源:网络整理
导读:我的 XML数据: dictionary version="0.8" revision="403605" grammemes grammeme parent=""POST/grammeme grammeme parent="POST"NOUN/grammeme /grammemes/dictionary 我的代码: type Dictionary struct { XMLName xml.Name `xml:"dictionary"` Grammemes
我的
XML数据:
<dictionary version="0.8" revision="403605"> <grammemes> <grammeme parent="">POST</grammeme> <grammeme parent="POST">NOUN</grammeme> </grammemes> </dictionary> 我的代码: type Dictionary struct { XMLName xml.Name `xml:"dictionary"` Grammemes *Grammemes `xml:"grammemes"` } type Grammemes struct { Grammemes []*Grammeme `xml:"grammeme"` } type Grammeme struct { Name string `xml:"grammeme"` Parent string `xml:"parent,attr"` } 我得到Grammeme.Parent属性,但我没有得到Grammeme.Name.为什么?
如果希望字段保存当前元素的内容,可以使用标记xml:“,chardata”.你标记你的结构的方式,而是寻找< grammeme>子元素.
因此,您可以解码的一组结构是: type Dictionary struct { XMLName xml.Name `xml:"dictionary"` Grammemes []Grammeme `xml:"grammemes>grammeme"` } type Grammeme struct { Name string `xml:",chardata"` Parent string `xml:"parent,attr"` } 你可以在这里测试这个例子:http://play.golang.org/p/7lQnQOCh0I (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |