Golang:xml解组无法正常工作
问题描述:
response getting in xml format:
<?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>
In GO i using following structure and code to decode
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"`
}
here i want to decode only link tag under entry tag. following is the code i using to decode the xml into necessary values
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)
but here output is only number of entries: {{http://www.w3.org/2005/Atom feed} []} dont know what is going wrong here please suggest any changes to make this work fine
</div>
答
The problem here is that you are using attr
where you shouldn't.
You are treating the XMLEntry
and XMLLink
tags as attributes.