This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
228 lines (206 loc) · 5.31 KB
/
example_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package xmltree
import (
"fmt"
"log"
)
func ExampleElement_Search() {
data := `
<Staff>
<Person>
<FullName>Ira Glass</FullName>
</Person>
<Person>
<FullName>Tom Magliozzi</FullName>
</Person>
<Person>
<FullName>Terry Gross</FullName>
</Person>
</Staff>
`
root, err := Parse([]byte(data))
if err != nil {
log.Fatal(err)
}
for _, el := range root.Search("", "FullName") {
fmt.Printf("%s\n", el.Content)
}
// Output:
// Ira Glass
// Tom Magliozzi
// Terry Gross
}
func ExampleElement_SearchFunc() {
data := `
<People>
<Person>
<FullName>Grace R. Emlin</FullName>
<Email where="home">
<Addr>[email protected]</Addr>
</Email>
<Email where='work'>
<Addr>[email protected]</Addr>
</Email>
</Person>
<Person>
<FullName>Michael P. Thompson</FullName>
<Email where="home">
<Addr>[email protected]</Addr>
</Email>
<Email where='work'>
<Addr>[email protected]</Addr>
<Addr>[email protected]</Addr>
</Email>
</Person>
</People>
`
root, err := Parse([]byte(data))
if err != nil {
log.Fatal(err)
}
workEmails := root.SearchFunc(func(el *Element) bool {
return el.Name.Local == "Email" && el.Attr("", "where") == "work"
})
for _, el := range workEmails {
for _, addr := range el.Children {
fmt.Printf("%s\n", addr.Content)
}
}
// Output:
}
func ExampleUnmarshal() {
var input = []byte(`<mediawiki xml:lang="en">
<page>
<title>Page title</title>
<restrictions>edit=sysop:move=sysop</restrictions>
<revision>
<timestamp>2001-01-15T13:15:00Z</timestamp>
<contributor><username>Foobar</username></contributor>
<comment>I have just one thing to say!</comment>
<text>A bunch of [[text]] here.</text>
<minor />
</revision>
<revision>
<timestamp>2001-01-15T13:10:27Z</timestamp>
<contributor><ip>10.0.0.2</ip></contributor>
<comment>new!</comment>
<text>An earlier [[revision]].</text>
</revision>
</page>
<page>
<title>Talk:Page title</title>
<revision>
<timestamp>2001-01-15T14:03:00Z</timestamp>
<contributor><ip>10.0.0.2</ip></contributor>
<comment>hey</comment>
<text>WHYD YOU LOCK PAGE??!!! i was editing that jerk</text>
</revision>
</page>
</mediawiki>`)
type revision struct {
Timestamp string `xml:"timestamp"`
Contributor string `xml:"contributor>ip"`
Comment string `xml:"comment"`
Text []string `xml:"text"`
}
root, err := Parse(input)
if err != nil {
log.Fatal(err)
}
// Pull all <revision> items from the input
for _, el := range root.Search("", "revision") {
var rev revision
if err := Unmarshal(el, &rev); err != nil {
log.Print(err)
continue
}
fmt.Println(rev.Timestamp, rev.Comment)
}
// Output:
// 2001-01-15T13:15:00Z I have just one thing to say!
// 2001-01-15T13:10:27Z new!
// 2001-01-15T14:03:00Z hey
}
func ExampleMarshal() {
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<toc>
<chapter-list>
<chapter>
<title>Civilizing Huck.Miss Watson.Tom Sawyer Waits.</title>
<number>1</number>
</chapter>
<chapter>
<title>The Boys Escape Jim.Torn Sawyer's Gang.Deep-laid Plans.</title>
<number>2</number>
</chapter>
<chapter>
<title>A Good Going-over.Grace Triumphant."One of Tom Sawyers's Lies".</title>
<number>3</number>
</chapter>
<chapter>
<title>Huck and the Judge.Superstition.</title>
<number>4</number>
</chapter>
</chapter-list>
</toc>`)
var chapters []Element
root, err := Parse(input)
if err != nil {
log.Fatal(err)
}
for _, el := range root.Search("", "chapter") {
for _, child := range el.Search("", "title") {
el.Content = child.Content
}
el.Children = nil
chapters = append(chapters, *el)
}
root.Children = chapters
fmt.Printf("%s\n", MarshalIndent(root, "", " "))
// Output:
// <toc>
// <chapter>Civilizing Huck.Miss Watson.Tom Sawyer Waits.</chapter>
// <chapter>The Boys Escape Jim.Torn Sawyer's Gang.Deep-laid Plans.</chapter>
// <chapter>A Good Going-over.Grace Triumphant."One of Tom Sawyers's Lies".</chapter>
// <chapter>Huck and the Judge.Superstition.</chapter>
// </toc>
}
func ExampleMarshalNested() {
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<toc>
<level1>
<level2>
<data>Hello World</data>
<justTheTag />
<number>3</number>
</level2>
<level2>
<data with-attr="1">Hello, again</data>
<justTheTag />
<number>12</number>
</level2>
</level1>
</toc>`)
parsed, err := Parse(input)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("%s\n", MarshalIndent(parsed, "", " "))
// Output:
// <toc>
// <level1>
// <level2>
// <data>Hello World</data>
// <justTheTag />
// <number>3</number>
// </level2>
// <level2>
// <data with-attr="1">Hello, again</data>
// <justTheTag />
// <number>12</number>
// </level2>
// </level1>
// </toc>
}