Golang:xml unmarshall 不按预期工作

huangapple go评论159阅读模式
英文:

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/&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 -->

在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 -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;feed xmlns=&quot;http://www.w3.org/2005/Atom&quot; xmlns:openSearch=&quot;http://a9.com/-/spec/opensearch/1.1/&quot;&gt;
&lt;id&gt;https://sites.google.com/feeds/activity/site/siteName&lt;/id&gt;
&lt;updated&gt;2009-09-10T05:24:23.120Z&lt;/updated&gt;
&lt;title&gt;Activity&lt;/title&gt;
&lt;link rel=&quot;alternate&quot; type=&quot;text/html&quot; href=&quot;http://sites.google.com/site/siteName/system/app/pages/recentChanges&quot;/&gt;
&lt;link rel=&quot;http://schemas.google.com/g/2005#feed&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/activity/site/siteName&quot;/&gt;
&lt;link rel=&quot;self&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/activity/site/siteName&quot;/&gt;
&lt;generator version=&quot;1&quot; uri=&quot;http://sites.google.com&quot;&gt;Google Sites&lt;/generator&gt;
&lt;openSearch:startIndex&gt;1&lt;/openSearch:startIndex&gt;
&lt;entry xmlns:gd=&quot;http://schemas.google.com/g/2005&quot; gd:etag=&quot;W/&amp;quot;CU4GQ3szfSl7ImA9WxNRFUg.&amp;quot;&quot;&gt;
&lt;id&gt;https://sites.google.com/feeds/activity/site/siteName/940375996952876062&lt;/id&gt;
&lt;updated&gt;2009-09-10T03:38:42.585Z&lt;/updated&gt;
&lt;category scheme=&quot;http://schemas.google.com/g/2005#kind&quot; term=&quot;http://schemas.google.com/sites/2008#deletion&quot; label=&quot;deletion&quot;/&gt;
&lt;title&gt;home&lt;/title&gt;
&lt;summary type=&quot;xhtml&quot;&gt;
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;User deleted &lt;a href=&quot;http://sites.google.com/site/siteName/home&quot;&gt;home&lt;/a&gt;
&lt;/div&gt;
&lt;/summary&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#revision&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/revision/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#current&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/content/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;self&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/activity/site/siteName/940375996952876062&quot;/&gt;
&lt;author&gt;
&lt;name&gt;User&lt;/name&gt;
&lt;email&gt;user@gmail.com&lt;/email&gt;
&lt;/author&gt;
&lt;/entry&gt;
&lt;entry xmlns:gd=&quot;http://schemas.google.com/g/2005&quot; gd:etag=&quot;W/&amp;quot;CU8DQn45fyl7ImA9WxNRFUg.&amp;quot;&quot;&gt;
&lt;id&gt;https://sites.google.com/feeds/activity/site/siteName/7165439066235480082&lt;/id&gt;
&lt;updated&gt;2009-09-10T03:37:53.027Z&lt;/updated&gt;
&lt;category scheme=&quot;http://schemas.google.com/g/2005#kind&quot;
term=&quot;http://schemas.google.com/sites/2008#edit&quot; label=&quot;edit&quot;/&gt;
&lt;title&gt;home&lt;/title&gt;
&lt;summary type=&quot;xhtml&quot;&gt;
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;User2 edited &lt;a href=&quot;http://sites.google.com/site/siteName/home&quot;&gt;home&lt;/a&gt;
&lt;/div&gt;
&lt;/summary&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#revision&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/revision/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#current&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/content/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;self&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/activity/site/siteName/7165439066235480082&quot;/&gt;
&lt;author&gt;
&lt;name&gt;User&lt;/name&gt;
&lt;email&gt;user@gmail.com&lt;/email&gt;
&lt;/author&gt;
&lt;/entry&gt;
&lt;entry xmlns:gd=&quot;http://schemas.google.com/g/2005&quot; gd:etag=&quot;W/&amp;quot;CU8AR3s4cSl7ImA9WxNRFUg.&amp;quot;&quot;&gt;
&lt;id&gt;https://sites.google.com/feeds/activity/site/siteName/127448462987345884&lt;/id&gt;
&lt;updated&gt;2009-09-10T03:37:26.539Z&lt;/updated&gt;
&lt;category scheme=&quot;http://schemas.google.com/g/2005#kind&quot;
term=&quot;http://schemas.google.com/sites/2008#creation&quot; label=&quot;creation&quot;/&gt;
&lt;title&gt;home&lt;/title&gt;
&lt;summary type=&quot;xhtml&quot;&gt;
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;User3 created &lt;a href=&quot;http://sites.google.com/site/siteName/home&quot;&gt;home&lt;/a&gt;
&lt;/div&gt;
&lt;/summary&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#revision&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/revision/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;http://schemas.google.com/sites/2008#current&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/content/site/siteName/5409745539831916487&quot;/&gt;
&lt;link rel=&quot;self&quot; type=&quot;application/atom+xml&quot;
href=&quot;https://sites.google.com/feeds/activity/site/siteName/127448462987345884&quot;/&gt;
&lt;author&gt;
&lt;name&gt;User3&lt;/name&gt;
&lt;email&gt;user3@gmail.com&lt;/email&gt;
&lt;/author&gt;
&lt;/entry&gt;
&lt;/feed&gt;

<!-- 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:&quot;link&quot;`
Rel     string   `xml:&quot;rel,attr&quot;`
Href    string   `xml:&quot;rel,href&quot;`
}
type XMLEntry struct {
XMLName xml.Name  `xml:&quot;entry&quot;`
Link    []XMLLink `xml:&quot;link,attr&quot;`
}
type XMLFeed struct {
XMLName xml.Name   `xml:&quot;feed&quot;`
Entry   []XMLEntry `xml:&quot;entry,attr&quot;`
}

<!-- 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, &amp;feed)
if err != nil {
log.Fatalf(&quot;Unable to unmarshall&quot;, err)
}
fmt.Println(&quot;number of entries:&quot;, 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的地方使用了它。

你把XMLEntryXMLLink标签当作属性来处理。

英文:

The problem here is that you are using attr where you shouldn't.

You are treating the XMLEntry and XMLLink tags as attributes.

huangapple
  • 本文由 发表于 2017年6月5日 16:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/44364959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定