<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="xmlns"</code>, <code>_xmlns:content="..."</code>, <code>_xmlns:atom="..."</code>. The code ...</p>
<pre><code>package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type Rss2 struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
XmlnsContent string `xml:"xmlns content,attr"`
XmlnsAtom string `xml:"xmlns atom,attr"`
Title string `xml:"channel>title"`
}
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, &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
{"XMLName":{"Space":"","Local":"rss"},"Version":"2.0","XmlnsContent":"","XmlnsAtom":"http://www.w3.org/2005/Atom","Title":"Latest news"}
<rss version="2.0" xmlns:_xmlns="xmlns" _xmlns:content="" _xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Latest news</title></channel></rss>
</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:"rss"`
Version string `xml:"version,attr"`
XmlnsContent string `xml:"xmlns:content,attr"`
XmlnsAtom string `xml:"xmlns:atom,attr"`
Title string `xml:"channel>title"`
}
</code></pre></pre>memorylane: <pre><p><a href="https://play.golang.org/p/sbSA1ZdZUh" rel="nofollow">That is brilliant!</a> Thank you! You don'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:"xmlns:content,attr"</code> and <code>xml:"xmlns:atom,attr"</code> now I get this output:</p>
<pre><code>$ go run rss-xmlns.go http://www.zdnet.com/news/rss.xml
{"XMLName":{"Space":"","Local":"rss"},"Version":"2.0","XmlnsContent":"","XmlnsAtom":"","Title":"Latest news"}
<rss version="2.0" xmlns:content="" xmlns:atom=""><channel><title>Latest news</title></channel></rss>
</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'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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传