example_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. package xml_test
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "os"
  9. )
  10. func ExampleMarshalIndent() {
  11. type Address struct {
  12. City, State string
  13. }
  14. type Person struct {
  15. XMLName xml.Name `xml:"person"`
  16. Id int `xml:"id,attr"`
  17. FirstName string `xml:"name>first"`
  18. LastName string `xml:"name>last"`
  19. Age int `xml:"age"`
  20. Height float32 `xml:"height,omitempty"`
  21. Married bool
  22. Address
  23. Comment string `xml:",comment"`
  24. }
  25. v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
  26. v.Comment = " Need more details. "
  27. v.Address = Address{"Hanga Roa", "Easter Island"}
  28. output, err := xml.MarshalIndent(v, " ", " ")
  29. if err != nil {
  30. fmt.Printf("error: %v\n", err)
  31. }
  32. os.Stdout.Write(output)
  33. // Output:
  34. // <person id="13">
  35. // <name>
  36. // <first>John</first>
  37. // <last>Doe</last>
  38. // </name>
  39. // <age>42</age>
  40. // <Married>false</Married>
  41. // <City>Hanga Roa</City>
  42. // <State>Easter Island</State>
  43. // <!-- Need more details. -->
  44. // </person>
  45. }
  46. func ExampleEncoder() {
  47. type Address struct {
  48. City, State string
  49. }
  50. type Person struct {
  51. XMLName xml.Name `xml:"person"`
  52. Id int `xml:"id,attr"`
  53. FirstName string `xml:"name>first"`
  54. LastName string `xml:"name>last"`
  55. Age int `xml:"age"`
  56. Height float32 `xml:"height,omitempty"`
  57. Married bool
  58. Address
  59. Comment string `xml:",comment"`
  60. }
  61. v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
  62. v.Comment = " Need more details. "
  63. v.Address = Address{"Hanga Roa", "Easter Island"}
  64. enc := xml.NewEncoder(os.Stdout)
  65. enc.Indent(" ", " ")
  66. if err := enc.Encode(v); err != nil {
  67. fmt.Printf("error: %v\n", err)
  68. }
  69. // Output:
  70. // <person id="13">
  71. // <name>
  72. // <first>John</first>
  73. // <last>Doe</last>
  74. // </name>
  75. // <age>42</age>
  76. // <Married>false</Married>
  77. // <City>Hanga Roa</City>
  78. // <State>Easter Island</State>
  79. // <!-- Need more details. -->
  80. // </person>
  81. }
  82. // This example demonstrates unmarshaling an XML excerpt into a value with
  83. // some preset fields. Note that the Phone field isn't modified and that
  84. // the XML <Company> element is ignored. Also, the Groups field is assigned
  85. // considering the element path provided in its tag.
  86. func ExampleUnmarshal() {
  87. type Email struct {
  88. Where string `xml:"where,attr"`
  89. Addr string
  90. }
  91. type Address struct {
  92. City, State string
  93. }
  94. type Result struct {
  95. XMLName xml.Name `xml:"Person"`
  96. Name string `xml:"FullName"`
  97. Phone string
  98. Email []Email
  99. Groups []string `xml:"Group>Value"`
  100. Address
  101. }
  102. v := Result{Name: "none", Phone: "none"}
  103. data := `
  104. <Person>
  105. <FullName>Grace R. Emlin</FullName>
  106. <Company>Example Inc.</Company>
  107. <Email where="home">
  108. <Addr>gre@example.com</Addr>
  109. </Email>
  110. <Email where='work'>
  111. <Addr>gre@work.com</Addr>
  112. </Email>
  113. <Group>
  114. <Value>Friends</Value>
  115. <Value>Squash</Value>
  116. </Group>
  117. <City>Hanga Roa</City>
  118. <State>Easter Island</State>
  119. </Person>
  120. `
  121. err := xml.Unmarshal([]byte(data), &v)
  122. if err != nil {
  123. fmt.Printf("error: %v", err)
  124. return
  125. }
  126. fmt.Printf("XMLName: %#v\n", v.XMLName)
  127. fmt.Printf("Name: %q\n", v.Name)
  128. fmt.Printf("Phone: %q\n", v.Phone)
  129. fmt.Printf("Email: %v\n", v.Email)
  130. fmt.Printf("Groups: %v\n", v.Groups)
  131. fmt.Printf("Address: %v\n", v.Address)
  132. // Output:
  133. // XMLName: xml.Name{Space:"", Local:"Person"}
  134. // Name: "Grace R. Emlin"
  135. // Phone: "none"
  136. // Email: [{home gre@example.com} {work gre@work.com}]
  137. // Groups: [Friends Squash]
  138. // Address: {Hanga Roa Easter Island}
  139. }