-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlunmarshal.go
More file actions
41 lines (38 loc) · 857 Bytes
/
xmlunmarshal.go
File metadata and controls
41 lines (38 loc) · 857 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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
func main() {
xmlFile,err:=os.Open("users.xml")
if err!=nil {
fmt.Println(err)
}
fmt.Println("successfully opened users.xml")
defer xmlFile.Close()
byteValue,_:=ioutil.ReadAll(xmlFile)
var users Users
xml.Unmarshal(byteValue,&users)
for i :=0;i<len(users.Users);i++ {
fmt.Println("User Address: "+users.Users[i].Address)
fmt.Println("User Name: "+users.Users[i].Name)
fmt.Println("Facebook Url: "+users.Users[i].Social.Email)
}
}
type Users struct {
XMLName xml.Name `xml:"users"`
Users []User `xml:"user"`
}
type User struct {
XMLName xml.Name `xml:"user"`
Address string `xml:"address,attr"`
Name string `xml:"name"`
Social Social `xml:"social"`
}
type Social struct {
XMLName xml.Name `xml:"social"`
Mobile string `xml:"mobile"`
Email string `xml:"email"`
}