-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_unmarshal_test.go
110 lines (102 loc) · 3.36 KB
/
example_unmarshal_test.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) VirtualTam
// SPDX-License-Identifier: MIT
package opml_test
import (
"encoding/json"
"fmt"
"os"
"github.com/virtualtam/opml-go"
)
func ExampleUnmarshal() {
blob := `<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>Feed subscriptions</title>
<ownerName>Jane Doe</ownerName>
<ownerEmail>[email protected]</ownerEmail>
</head>
<body>
<outline text="Linux" title="Linux">
<outline text="Bits from Debian" htmlUrl="https://bits.debian.org/feeds/atom.xml" title="Bits from Debian" type="rss" xmlUrl="https://bits.debian.org/feeds/atom.xml"></outline>
<outline text="KXStudio News" htmlUrl="https://kx.studio/News" title="KXStudio News" type="rss" xmlUrl="https://kx.studio/News/?action=feed"></outline>
</outline>
<outline text="Social News" title="Social News">
<outline text="Hacker News" htmlUrl="https://news.ycombinator.com/" title="Hacker News" type="rss" xmlUrl="https://news.ycombinator.com/rss"></outline>
<outline text="Lobsters" htmlUrl="https://lobste.rs" title="Lobsters" type="rss" xmlUrl="https://lobste.rs/rss"></outline>
<outline text="Phoronix" htmlUrl="https://www.phoronix.com/" title="Phoronix" type="rss" xmlUrl="https://www.phoronix.com/rss.php"></outline>
</outline>
</body>
</opml>
`
document, err := opml.Unmarshal([]byte(blob))
if err != nil {
fmt.Println("failed to unmarshal file:", err)
os.Exit(1)
}
jsonData, err := json.MarshalIndent(document, "", " ")
if err != nil {
fmt.Println("failed to marshal data as JSON:", err)
os.Exit(1)
}
fmt.Println(string(jsonData))
// Output:
// {
// "version": "2.0",
// "head": {
// "title": "Feed subscriptions",
// "owner_name": "Jane Doe",
// "owner_email": "[email protected]"
// },
// "body": {
// "outlines": [
// {
// "text": "Linux",
// "title": "Linux",
// "outlines": [
// {
// "text": "Bits from Debian",
// "html_url": "https://bits.debian.org/feeds/atom.xml",
// "title": "Bits from Debian",
// "type": "rss",
// "xml_url": "https://bits.debian.org/feeds/atom.xml"
// },
// {
// "text": "KXStudio News",
// "html_url": "https://kx.studio/News",
// "title": "KXStudio News",
// "type": "rss",
// "xml_url": "https://kx.studio/News/?action=feed"
// }
// ]
// },
// {
// "text": "Social News",
// "title": "Social News",
// "outlines": [
// {
// "text": "Hacker News",
// "html_url": "https://news.ycombinator.com/",
// "title": "Hacker News",
// "type": "rss",
// "xml_url": "https://news.ycombinator.com/rss"
// },
// {
// "text": "Lobsters",
// "html_url": "https://lobste.rs",
// "title": "Lobsters",
// "type": "rss",
// "xml_url": "https://lobste.rs/rss"
// },
// {
// "text": "Phoronix",
// "html_url": "https://www.phoronix.com/",
// "title": "Phoronix",
// "type": "rss",
// "xml_url": "https://www.phoronix.com/rss.php"
// }
// ]
// }
// ]
// }
// }
}