Golang:XML解组未按预期

问题描述:

响应xml格式获得:Golang: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/&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>[email protected]</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>[email protected]</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>[email protected]</email> 
 
    </author> 
 
</entry> 
 
</feed>

在GO我使用以下结构和代码解码

type XMLLink struct { 
 
\t XMLName xml.Name `xml:"link"` 
 
\t Rel  string `xml:"rel,attr"` 
 
\t Href string `xml:"rel,href"` 
 
} 
 
type XMLEntry struct { 
 
\t XMLName xml.Name `xml:"entry"` 
 
\t Link []XMLLink `xml:"link,attr"` 
 
} 
 
type XMLFeed struct { 
 
\t XMLName xml.Name `xml:"feed"` 
 
\t Entry []XMLEntry `xml:"entry,attr"` 
 
}

这里我想解码条目标签下的链接标签。 以下为i使用与XML解码成必要的值

var feed XMLFeed 
 
//response got from http request is expected xml here which is already men tioned above 
 
bodyBytes, _ := ioutil.ReadAll(response.Body) 
 
\t \t \t err := xml.Unmarshal(bodyBytes, &feed) 
 
\t \t \t if err != nil { 
 
\t \t \t \t log.Fatalf("Unable to unmarshall", err) 
 
\t \t \t } 
 
\t \t \t fmt.Println("number of entries:", feed)

但这里输出的代码只 数目的条目:{{http://www.w3.org/2005/Atom饲料} []} 不知道这里出了什么问题,请提出任何更改,以使此工作正常

这里的问题是,您正在使用attr你笑的地方uldn't。

您正在将XMLEntryXMLLink标记视为属性。