This post shows how to parse XML of RSS 2.0 format. The
example RSS 2.0 feed comes from Solidot website.
Souce Code
Run code on Go Playground
parse-6.go |
repository |
view raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 | // http://www.w3schools.com/rss/default.asp
// http://www.tutorialspoint.com/rss/what-is-atom.htm
// http://stackoverflow.com/questions/16309944/atom-to-rss-feeds-converter
package main
import (
"io/ioutil"
"encoding/xml"
"html/template"
"fmt"
)
// http://www.w3schools.com/rss/rss_syntax.asp
// http://www.w3schools.com/rss/rss_channel.asp
type Rss2 struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
// Required
Title string `xml:"channel>title"`
Link string `xml:"channel>link"`
Description string `xml:"channel>description"`
// Optional
PubDate string `xml:"channel>pubDate"`
ItemList []Item `xml:"channel>item"`
}
// http://www.w3schools.com/rss/rss_item.asp
// http://stackoverflow.com/questions/7220670/difference-between-description-and-contentencoded-tags-in-rss2
// https://groups.google.com/d/topic/golang-nuts/uBMo1BpaQCM
type Item struct {
// Required
Title string `xml:"title"`
Link string `xml:"link"`
Description template.HTML `xml:"description"`
// Optional
Content template.HTML `xml:"encoded"`
PubDate string `xml:"pubDate"`
Comments string `xml:"comments"`
}
func main() {
r := Rss2{}
xmlContent, _ := ioutil.ReadFile("example-6.xml")
err := xml.Unmarshal(xmlContent, &r)
if err != nil { panic(err) }
for _, item := range r.ItemList {
fmt.Println(item)
}
}
|
Note
The following struct fields in Item strcut have the type template.HTML
Description template.HTML `xml:"description"`
Content template.HTML `xml:"encoded"`
If template.HTML are replaced with string, the result is the same.
Tested on: Ubuntu Linux 14.10, Go 1.4.
[Golang] XML Parsing Example series:
Reference: