[Golang] XML Parsing Example (5) - Parse OPML


Based on previous examples, this post shows how to parse OPML file. The following OPML example comes from my web feeds:

example-5.xml | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
  <title>Feeder - RSS Feed Reader</title>
</head>
<body>
  <outline text="Hacker News" title="Hacker News" type="rss" xmlUrl="https://news.ycombinator.com/rss" htmlUrl="https://news.ycombinator.com/" rssfr-favicon="https://news.ycombinator.com/favicon.ico"/>
  <outline text="CSDN最新资讯" title="CSDN最新资讯" type="rss" xmlUrl="http://www.csdn.net/article/rss_lastnews" htmlUrl="http://news.csdn.net" rssfr-favicon="http://s2.googleusercontent.com/s2/favicons?domain=csdn.net"/>
  <outline text="Solidot" title="Solidot" type="rss" xmlUrl="http://www.solidot.org/index.rss" htmlUrl="http://www.solidot.org" rssfr-favicon="http://s2.googleusercontent.com/s2/favicons?domain=solidot.org"/>
  <outline text="Slashdot" title="Slashdot" type="rss" xmlUrl="http://rss.slashdot.org/Slashdot/slashdot" htmlUrl="http://slashdot.org/" rssfr-favicon="http://slashdot.org/favicon.ico"/>
  <outline text="InfoQ中文" title="InfoQ中文" type="rss" xmlUrl="http://www.infoq.com/cn/rss/rss.action?token=Apz6T7JrRXKONbiLoKzdoHiJ8uTgu5ef" htmlUrl="http://www.infoq.com/cn/" rssfr-favicon="http://infoqstatic.com/favicon.ico"/>
  <outline text="外刊IT评论" title="外刊IT评论" type="rss" xmlUrl="http://www.aqee.net/feed/" htmlUrl="http://www.aqee.net" rssfr-favicon="http://s2.googleusercontent.com/s2/favicons?domain=aqee.net"/>
  <outline text="博客 - 伯乐在线" title="博客 - 伯乐在线" type="rss" xmlUrl="http://blog.jobbole.com/feed/" htmlUrl="http://blog.jobbole.com" rssfr-favicon="http://cdn2.jobbole.com/2013/10/favicon.png"/>
</body>
</opml>

Source code for parsing above OPML:

Run code on Go Playground

parse-5.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
package main

import (
	"io/ioutil"
	"encoding/xml"
	"fmt"
)

type opml struct {
	XMLName		xml.Name	`xml:"opml"`
	Version		string		`xml:"version,attr"`
	Head		head
	Body		body
}

type head struct {
	XMLName		xml.Name	`xml:"head"`
	Title		string		`xml:"title"`
}

type body struct {
	XMLName		xml.Name	`xml:"body"`
	Outlines	[]outline	`xml:"outline"`
}

type outline struct {
	Text		string		`xml:"text,attr"`
	Title		string		`xml:"title,attr"`
	Type		string		`xml:"type,attr"`
	XmlUrl		string		`xml:"xmlUrl,attr"`
	HtmlUrl		string		`xml:"htmlUrl,attr"`
	Favicon		string		`xml:"rssfr-favicon,attr"`
}

func main() {
	o := opml{}
	xmlContent, _ := ioutil.ReadFile("example-5.xml")
	err := xml.Unmarshal(xmlContent, &o)
	if err != nil { panic(err) }
	for _, outline := range o.Body.Outlines {
		fmt.Println(outline)
	}
}

The output result:

{Hacker News Hacker News rss https://news.ycombinator.com/rss https://news.ycombinator.com/ https://news.ycombinator.com/favicon.ico}
{CSDN最新资讯 CSDN最新资讯 rss http://www.csdn.net/article/rss_lastnews http://news.csdn.net http://s2.googleusercontent.com/s2/favicons?domain=csdn.net}
{Solidot Solidot rss http://www.solidot.org/index.rss http://www.solidot.org http://s2.googleusercontent.com/s2/favicons?domain=solidot.org}
{Slashdot Slashdot rss http://rss.slashdot.org/Slashdot/slashdot http://slashdot.org/ http://slashdot.org/favicon.ico}
{InfoQ中文 InfoQ中文 rss http://www.infoq.com/cn/rss/rss.action?token=Apz6T7JrRXKONbiLoKzdoHiJ8uTgu5ef http://www.infoq.com/cn/ http://infoqstatic.com/favicon.ico}
{外刊IT评论 外刊IT评论 rss http://www.aqee.net/feed/ http://www.aqee.net http://s2.googleusercontent.com/s2/favicons?domain=aqee.net}
{博客 - 伯乐在线 博客 - 伯乐在线 rss http://blog.jobbole.com/feed/ http://blog.jobbole.com http://cdn2.jobbole.com/2013/10/favicon.png}

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]OPML
[b]XML to Go struct : golang