以xml格式获取响应:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
<id>https://sites.google.com/feeds/activity/site/siteName</id>
<updated>2009-09-10T05:24:23.120Z</updated>
<title>Activity</title>
<link rel="alternate" type="text/html" href="http://sites.google.com/site/siteName/system/app/pages/recentChanges"/>
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"
href="https://sites.google.com/feeds/activity/site/siteName"/>
<link rel="self" type="application/atom+xml"
href="https://sites.google.com/feeds/activity/site/siteName"/>
<generator version="1" uri="http://sites.google.com">Google Sites</generator>
<openSearch:startIndex>1</openSearch:startIndex>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"CU4GQ3szfSl7ImA9WxNRFUg."">
<id>https://sites.google.com/feeds/activity/site/siteName/940375996952876062</id>
<updated>2009-09-10T03:38:42.585Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/sites/2008#deletion" label="deletion"/>
<title>home</title>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">User deleted <a href="http://sites.google.com/site/siteName/home">home</a>
</div>
</summary>
<link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml"
href="https://sites.google.com/feeds/revision/site/siteName/5409745539831916487"/>
<link rel="http://schemas.google.com/sites/2008#current" type="application/atom+xml"
href="https://sites.google.com/feeds/content/site/siteName/5409745539831916487"/>
<link rel="self" type="application/atom+xml"
href="https://sites.google.com/feeds/activity/site/siteName/940375996952876062"/>
<author>
<name>User</name>
<email>user@gmail.com</email>
</author>
</entry>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"CU8DQn45fyl7ImA9WxNRFUg."">
<id>https://sites.google.com/feeds/activity/site/siteName/7165439066235480082</id>
<updated>2009-09-10T03:37:53.027Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/sites/2008#edit" label="edit"/>
<title>home</title>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">User2 edited <a href="http://sites.google.com/site/siteName/home">home</a>
</div>
</summary>
<link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml"
href="https://sites.google.com/feeds/revision/site/siteName/5409745539831916487"/>
<link rel="http://schemas.google.com/sites/2008#current" type="application/atom+xml"
href="https://sites.google.com/feeds/content/site/siteName/5409745539831916487"/>
<link rel="self" type="application/atom+xml"
href="https://sites.google.com/feeds/activity/site/siteName/7165439066235480082"/>
<author>
<name>User</name>
<email>user@gmail.com</email>
</author>
</entry>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"CU8AR3s4cSl7ImA9WxNRFUg."">
<id>https://sites.google.com/feeds/activity/site/siteName/127448462987345884</id>
<updated>2009-09-10T03:37:26.539Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/sites/2008#creation" label="creation"/>
<title>home</title>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">User3 created <a href="http://sites.google.com/site/siteName/home">home</a>
</div>
</summary>
<link rel="http://schemas.google.com/sites/2008#revision" type="application/atom+xml"
href="https://sites.google.com/feeds/revision/site/siteName/5409745539831916487"/>
<link rel="http://schemas.google.com/sites/2008#current" type="application/atom+xml"
href="https://sites.google.com/feeds/content/site/siteName/5409745539831916487"/>
<link rel="self" type="application/atom+xml"
href="https://sites.google.com/feeds/activity/site/siteName/127448462987345884"/>
<author>
<name>User3</name>
<email>user3@gmail.com</email>
</author>
</entry>
</feed>
在GO中使用以下结构和代码进行解码
type XMLLink struct {
XMLName xml.Name `xml:"link"`
Rel string `xml:"rel,attr"`
Href string `xml:"rel,href"`
}
type XMLEntry struct {
XMLName xml.Name `xml:"entry"`
Link []XMLLink `xml:"link,attr"`
}
type XMLFeed struct {
XMLName xml.Name `xml:"feed"`
Entry []XMLEntry `xml:"entry,attr"`
}
这里我只想解码入口标签下的链接标签。 以下是我用来将xml解码为必要值的代码
var feed XMLFeed
//response got from http request is expected xml here which is already men tioned above
bodyBytes, _ := ioutil.ReadAll(response.Body)
err := xml.Unmarshal(bodyBytes, &feed)
if err != nil {
log.Fatalf("Unable to unmarshall", err)
}
fmt.Println("number of entries:", feed)
但这里仅输出 条目数:{{http://www.w3.org/2005/Atom Feed} []} 不知道这里出了什么问题,请建议任何改变,以使这项工作正常
答案 0 :(得分:0)
问题在于您使用的是attr
。
您将XMLEntry
和XMLLink
标记视为属性。