why do I get “_xmlns” when marshalling an RSS struct?

polaris · · 494 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>After reading a RSS feed, I want to marshall it again (e. g. after filtering some items), but I get an weird <code>xmlns:_xmlns=&#34;xmlns&#34;</code>, <code>_xmlns:content=&#34;...&#34;</code>, <code>_xmlns:atom=&#34;...&#34;</code>. The code ...</p> <pre><code>package main import ( &#34;encoding/json&#34; &#34;encoding/xml&#34; &#34;fmt&#34; &#34;io/ioutil&#34; &#34;log&#34; &#34;net/http&#34; &#34;os&#34; ) type Rss2 struct { XMLName xml.Name `xml:&#34;rss&#34;` Version string `xml:&#34;version,attr&#34;` XmlnsContent string `xml:&#34;xmlns content,attr&#34;` XmlnsAtom string `xml:&#34;xmlns atom,attr&#34;` Title string `xml:&#34;channel&gt;title&#34;` } func main() { var err error response, err := http.Get(os.Args[1]) if err != nil { log.Fatal(err) } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatal(err) } rss := Rss2{} err = xml.Unmarshal(body, &amp;rss) // all fine ... json, err := json.Marshal(rss) fmt.Println(); fmt.Println(string(json)) // strange _xmlns ... marshalled, err := xml.Marshal(rss) fmt.Println(); fmt.Println(string(marshalled)) } </code></pre> <p>gives me this output:</p> <pre><code>$ go run rss-xmlns.go http://www.zdnet.com/news/rss.xml {&#34;XMLName&#34;:{&#34;Space&#34;:&#34;&#34;,&#34;Local&#34;:&#34;rss&#34;},&#34;Version&#34;:&#34;2.0&#34;,&#34;XmlnsContent&#34;:&#34;&#34;,&#34;XmlnsAtom&#34;:&#34;http://www.w3.org/2005/Atom&#34;,&#34;Title&#34;:&#34;Latest news&#34;} &lt;rss version=&#34;2.0&#34; xmlns:_xmlns=&#34;xmlns&#34; _xmlns:content=&#34;&#34; _xmlns:atom=&#34;http://www.w3.org/2005/Atom&#34;&gt;&lt;channel&gt;&lt;title&gt;Latest news&lt;/title&gt;&lt;/channel&gt;&lt;/rss&gt; </code></pre> <p>... any idea why? What did I miss?</p> <hr/>**评论:**<br/><br/>JHunz: <pre><p>Pretty sure your tags are just wrong. </p> <pre><code>type Rss2 struct { XMLName xml.Name `xml:&#34;rss&#34;` Version string `xml:&#34;version,attr&#34;` XmlnsContent string `xml:&#34;xmlns:content,attr&#34;` XmlnsAtom string `xml:&#34;xmlns:atom,attr&#34;` Title string `xml:&#34;channel&gt;title&#34;` } </code></pre></pre>memorylane: <pre><p><a href="https://play.golang.org/p/sbSA1ZdZUh" rel="nofollow">That is brilliant!</a> Thank you! You don&#39;t know how much pain xmlns reproduction has caused me. I never thought about using <code>:</code> in the tags because they differ in marshaling and un-marshaling.</p></pre>coke4all: <pre><p>unfortunately this does not solve the problem: with <code>xml:&#34;xmlns:content,attr&#34;</code> and <code>xml:&#34;xmlns:atom,attr&#34;</code> now I get this output:</p> <pre><code>$ go run rss-xmlns.go http://www.zdnet.com/news/rss.xml {&#34;XMLName&#34;:{&#34;Space&#34;:&#34;&#34;,&#34;Local&#34;:&#34;rss&#34;},&#34;Version&#34;:&#34;2.0&#34;,&#34;XmlnsContent&#34;:&#34;&#34;,&#34;XmlnsAtom&#34;:&#34;&#34;,&#34;Title&#34;:&#34;Latest news&#34;} &lt;rss version=&#34;2.0&#34; xmlns:content=&#34;&#34; xmlns:atom=&#34;&#34;&gt;&lt;channel&gt;&lt;title&gt;Latest news&lt;/title&gt;&lt;/channel&gt;&lt;/rss&gt; </code></pre> <p>... the marshalling (second line, the XML output) seems to be okay, but the unmarshalling of the RSS feed (first line, the JSON output) prints an empty <code>XmlnsAtom</code>. I don&#39;t have to create two structures, one with space for unmarshalling, one with <code>:</code> for marshalling?! Do I have to?!?</p></pre>memorylane: <pre><p>Yes, as far as I can tell, you need two structures if you want to reproduce the xmlns declarations when marshalling. If you don&#39;t care about recreating those then you can use one structure.</p></pre>memorylane: <pre><p>The <code>_</code> is prepended by <a href="https://github.com/golang/go/blob/master/src/encoding/xml/marshal.go#L341-L344" rel="nofollow">this code</a> which checks if your prefix starts with xml.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

494 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传