-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding_xml_Unmarshal.go
More file actions
43 lines (43 loc) · 908 Bytes
/
encoding_xml_Unmarshal.go
File metadata and controls
43 lines (43 loc) · 908 Bytes
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 (
"encoding/xml"
"log"
)
type Result struct {
XMLName xml.Name `xml:"persons"`
Persons []Person `xml:"person"`
}
type Person struct {
Name string `xml:"name,attr"`
Age int `xml:"age,attr"`
Career string `xml:"career"`
Interests []string `xml:"interests>interest"`
}
func main() {
content := []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person name="polaris" age="28">
<career>无业游民</career>
<interests>
<interest>编程</interest>
<interest>下棋</interest>
</interests>
</person>
<person name="studygolang" age="27">
<career>码农</career>
<interests>
<interest>编程</interest>
<interest>下棋</interest>
</interests>
</person>
</persons>
`)
var result Result
err := xml.Unmarshal(content, &result)
if err != nil {
log.Fatal(err)
}
log.Println(result)
log.Println(result.Persons[0].Name)
}