[Golang] XML Parsing Example (8) - Parse Atom 1.0


This post shows how to parse XML of Atom 1.0 format. The example Atom 1.0 feed comes from kura.io website.

Souce Code

Run code on Go Playground

parse-7.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
52
53
// 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"
	"fmt"
)


// http://en.wikipedia.org/wiki/Atom_%28standard%29
// http://golang.org/src/pkg/encoding/xml/
type Atom1 struct {
	XMLName		xml.Name	`xml:"http://www.w3.org/2005/Atom feed"`
	Title		string		`xml:"title"`
	Subtitle	string		`xml:"subtitle"`
	Id		string		`xml:"id"`
	Updated		string		`xml:"updated"`
	Rights		string		`xml:"rights"`
	Link		Link		`xml:"link"`
	Author		Author		`xml:"author"`
	EntryList	[]Entry		`xml:"entry"`
}

type Link struct {
	Href		string		`xml:"href,attr"`
}

type Author struct {
	Name		string		`xml:"name"`
	Email		string		`xml:"email"`
}

type Entry struct {
	Title		string		`xml:"title"`
	Summary		string		`xml:"summary"`
	Content		string		`xml:"content"`
	Id		string		`xml:"id"`
	Updated		string		`xml:"updated"`
	Link		Link		`xml:"link"`
	Author		Author		`xml:"author"`
}

func main() {
	a := Atom1{}
	xmlContent, _ := ioutil.ReadFile("example-7.xml")
	err := xml.Unmarshal(xmlContent, &a)
	if err != nil { panic(err) }
	for _, entry := range a.EntryList {
		fmt.Println(entry)
	}
}

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]What is Atom 1.0
[b]php - ATOM to RSS feeds converter - Stack Overflow
[c]Atom (standard) - Wikipedia, the free encyclopedia
[d]src/encoding/xml/ - The Go Programming Language
[e][Golang] Convert Atom to RSS
[f]XML to Go struct : golang