[Golang] XML Parsing Example (7) - Parse RSS 2.0


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:

[1][Golang] XML Parsing Example (1)
[2][Golang] XML Parsing Example (2)
[3][Golang] XML Parsing Example (3)
[4][Golang] XML Parsing Example (4)
[5][Golang] XML Parsing Example (5) - Parse OPML
[6][Golang] XML Parsing Example (6) - Parse OPML Concisely
[7][Golang] XML Parsing Example (7) - Parse RSS 2.0
[8][Golang] XML Parsing Example (8) - Parse Atom 1.0
[9][Golang] Convert Atom to RSS
[10][Golang] Parse Web Feed - RSS and Atom

Reference:

[a]RSS Tutorial - w3schools.com
[b]What is Atom 1.0
[c]php - ATOM to RSS feeds converter - Stack Overflow
[d]RSS Syntax - w3schools.com
[e]RSS <channel> Element - w3schools.com
[f]RSS <item> Element - w3schools.com
[g]xml - Difference between description and content:encoded tags in RSS2 - Stack Overflow
[h]golang xml parsing with CDATA and a colon in the keyname - Google Groups
[i][Golang] Convert Atom to RSS
[j]XML to Go struct : golang