github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/encoding/xml/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package xml_test 6 7 import ( 8 "github.com/shogo82148/std/encoding/xml" 9 "github.com/shogo82148/std/fmt" 10 "github.com/shogo82148/std/os" 11 ) 12 13 func ExampleMarshalIndent() { 14 type Address struct { 15 City, State string 16 } 17 type Person struct { 18 XMLName xml.Name `xml:"person"` 19 Id int `xml:"id,attr"` 20 FirstName string `xml:"name>first"` 21 LastName string `xml:"name>last"` 22 Age int `xml:"age"` 23 Height float32 `xml:"height,omitempty"` 24 Married bool 25 Address 26 Comment string `xml:",comment"` 27 } 28 29 v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42} 30 v.Comment = " Need more details. " 31 v.Address = Address{"Hanga Roa", "Easter Island"} 32 33 output, err := xml.MarshalIndent(v, " ", " ") 34 if err != nil { 35 fmt.Printf("error: %v\n", err) 36 } 37 38 os.Stdout.Write(output) 39 // Output: 40 // <person id="13"> 41 // <name> 42 // <first>John</first> 43 // <last>Doe</last> 44 // </name> 45 // <age>42</age> 46 // <Married>false</Married> 47 // <City>Hanga Roa</City> 48 // <State>Easter Island</State> 49 // <!-- Need more details. --> 50 // </person> 51 } 52 53 func ExampleEncoder() { 54 type Address struct { 55 City, State string 56 } 57 type Person struct { 58 XMLName xml.Name `xml:"person"` 59 Id int `xml:"id,attr"` 60 FirstName string `xml:"name>first"` 61 LastName string `xml:"name>last"` 62 Age int `xml:"age"` 63 Height float32 `xml:"height,omitempty"` 64 Married bool 65 Address 66 Comment string `xml:",comment"` 67 } 68 69 v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42} 70 v.Comment = " Need more details. " 71 v.Address = Address{"Hanga Roa", "Easter Island"} 72 73 enc := xml.NewEncoder(os.Stdout) 74 enc.Indent(" ", " ") 75 if err := enc.Encode(v); err != nil { 76 fmt.Printf("error: %v\n", err) 77 } 78 79 // Output: 80 // <person id="13"> 81 // <name> 82 // <first>John</first> 83 // <last>Doe</last> 84 // </name> 85 // <age>42</age> 86 // <Married>false</Married> 87 // <City>Hanga Roa</City> 88 // <State>Easter Island</State> 89 // <!-- Need more details. --> 90 // </person> 91 } 92 93 // この例では、XMLの一部をあらかじめ設定されたフィールドを持つ値にアンマーシャルする方法を示しています。 94 // Phoneフィールドが変更されず、XMLの<Company>要素が無視されることに注意してください。 95 // また、Groupsフィールドは、そのタグに提供された要素パスを考慮して割り当てられます。 96 func ExampleUnmarshal() { 97 type Email struct { 98 Where string `xml:"where,attr"` 99 Addr string 100 } 101 type Address struct { 102 City, State string 103 } 104 type Result struct { 105 XMLName xml.Name `xml:"Person"` 106 Name string `xml:"FullName"` 107 Phone string 108 Email []Email 109 Groups []string `xml:"Group>Value"` 110 Address 111 } 112 v := Result{Name: "none", Phone: "none"} 113 114 data := ` 115 <Person> 116 <FullName>Grace R. Emlin</FullName> 117 <Company>Example Inc.</Company> 118 <Email where="home"> 119 <Addr>gre@example.com</Addr> 120 </Email> 121 <Email where='work'> 122 <Addr>gre@work.com</Addr> 123 </Email> 124 <Group> 125 <Value>Friends</Value> 126 <Value>Squash</Value> 127 </Group> 128 <City>Hanga Roa</City> 129 <State>Easter Island</State> 130 </Person> 131 ` 132 err := xml.Unmarshal([]byte(data), &v) 133 if err != nil { 134 fmt.Printf("error: %v", err) 135 return 136 } 137 fmt.Printf("XMLName: %#v\n", v.XMLName) 138 fmt.Printf("Name: %q\n", v.Name) 139 fmt.Printf("Phone: %q\n", v.Phone) 140 fmt.Printf("Email: %v\n", v.Email) 141 fmt.Printf("Groups: %v\n", v.Groups) 142 fmt.Printf("Address: %v\n", v.Address) 143 // Output: 144 // XMLName: xml.Name{Space:"", Local:"Person"} 145 // Name: "Grace R. Emlin" 146 // Phone: "none" 147 // Email: [{home gre@example.com} {work gre@work.com}] 148 // Groups: [Friends Squash] 149 // Address: {Hanga Roa Easter Island} 150 }