英文:
Golang:xml unmarshall not working as expected
问题
响应以XML格式获取:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?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>
<!-- end snippet -->
在Go中,我使用以下结构和代码进行解码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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"`
}
<!-- end snippet -->
在这里,我只想解码entry标签下的link标签。
以下是我用于将XML解码为所需值的代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
var feed XMLFeed
// response是从http请求获取的预期xml,如上所述
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)
<!-- end snippet -->
但是这里的输出只有
number of entries: {{http://www.w3.org/2005/Atom feed} []}
不知道出了什么问题,请建议如何更改以使其正常工作。
英文:
response getting in xml format:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?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/&quot;CU4GQ3szfSl7ImA9WxNRFUg.&quot;">
<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/&quot;CU8DQn45fyl7ImA9WxNRFUg.&quot;">
<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/&quot;CU8AR3s4cSl7ImA9WxNRFUg.&quot;">
<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>
<!-- end snippet -->
In GO i using following structure and code to decode
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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"`
}
<!-- end snippet -->
here i want to decode only link tag under entry tag.
following is the code i using to decode the xml into necessary values
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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)
<!-- end snippet -->
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
答案1
得分: 0
问题在于你在不应该使用attr
的地方使用了它。
你把XMLEntry
和XMLLink
标签当作属性来处理。
英文:
The problem here is that you are using attr
where you shouldn't.
You are treating the XMLEntry
and XMLLink
tags as attributes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论