-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathxml_test.go
More file actions
49 lines (45 loc) · 919 Bytes
/
xml_test.go
File metadata and controls
49 lines (45 loc) · 919 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
44
45
46
47
48
49
package xml
import (
"encoding/xml"
"log"
"testing"
)
/**
对象转xml
*/
type Address struct {
City string
State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func TestXml(t *testing.T) {
addr := Address{City: "上海", State: "中国"}
p := &Person{Id: 13,
FirstName: "Pi",
LastName: "bigstar",
Age: 20, Height: 17.8,
Married: true,
Address: addr,
Comment: "我是注释",
}
// 对象转xml
personXML, err := xml.Marshal(p)
if err != nil {
log.Println("Error when marshal", err.Error())
}
t.Log(string(personXML))
//xml 文本转 对象
obj := &Person{}
xml.Unmarshal(personXML, obj)
t.Logf("%+v", obj)
}