我到处都在搜索,无法找到解决方案。
我正在尝试从Wikipedia中获取嵌套xml标记的属性,但到目前为止无法获取嵌套的属性。我特别想pageid
从page
标签中获取。
这是xml的示例:
<api batchcomplete="">
<query>
<pages>
<page _idx="25039021" pageid="25039021" ns="0" title="Go (programming language)">
<extract xml:space="preserve">
stuff about golang
</extract>
</page>
</pages>
</query>
</api>
这是我正在使用的结构:
type Page struct {
PageID string `xml:"pageid,attr"`
}
type Extract struct {
Text string `xml:"query>pages>page>extract"`
Page Page `xml:"query>pages>page"`
}
但extract.page.pageid仍然没有返回。我有什么想念/做错了吗?
您的结构应更改为:
type Page struct {
PageID string `xml:"pageid,attr"`
Text string `xml:"extract"`
}
type Extract struct {
Page Page `xml:"query>pages>page"`
}
fmt.Println(extract.Page.PageID)
fmt.Println(extract.Page.Text)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句