src/pkg/encoding/xml/example_test.go | 34 ++++++++++++++++++++++++----------
src/pkg/encoding/xml/marshal.go | 2 ++
src/pkg/encoding/xml/read.go | 3 +++
diff --git a/src/pkg/encoding/xml/example_test.go b/src/pkg/encoding/xml/example_test.go
index 6c5390766b10d6fda015183b7bd2518ce6dd6214..082ce6803d3b908289173fc23b9791ec31a9d79f 100644
--- a/src/pkg/encoding/xml/example_test.go
+++ b/src/pkg/encoding/xml/example_test.go
@@ -11,6 +11,9 @@ "os"
)
func ExampleMarshalIndent() {
+ type Address struct {
+ City, State string
+ }
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
@@ -19,11 +22,13 @@ LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
- Comment string `xml:",comment"`
+ Address
+ Comment string `xml:",comment"`
}
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
- v.Comment = " Need more fields. "
+ v.Comment = " Need more details. "
+ v.Address = Address{"Hanga Roa", "Easter Island"}
output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
@@ -39,7 +44,9 @@ // Doe
//
// 42
// false
- //
+ // Hanga Roa
+ // Easter Island
+ //
//
}
@@ -52,14 +59,19 @@ type Email struct {
Where string `xml:"where,attr"`
Addr string
}
+ type Address struct {
+ City, State string
+ }
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Phone string
Email []Email
Groups []string `xml:"Group>Value"`
+ Address
}
- p := Result{Name: "none", Phone: "none"}
+ v := Result{Name: "none", Phone: "none"}
+ v.Address = Address{"Hanga Roa", "Easter Island"}
data := `
@@ -77,20 +89,22 @@
123 Main Street
`
- err := xml.Unmarshal([]byte(data), &p)
+ err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
- fmt.Printf("XMLName: %#v\n", p.XMLName)
- fmt.Printf("Name: %q\n", p.Name)
- fmt.Printf("Phone: %q\n", p.Phone)
- fmt.Printf("Email: %v\n", p.Email)
- fmt.Printf("Groups: %v\n", p.Groups)
+ fmt.Printf("XMLName: %#v\n", v.XMLName)
+ fmt.Printf("Name: %q\n", v.Name)
+ fmt.Printf("Phone: %q\n", v.Phone)
+ fmt.Printf("Email: %v\n", v.Email)
+ fmt.Printf("Groups: %v\n", v.Groups)
+ fmt.Printf("Address: %v\n", v.Address)
// Output:
// XMLName: xml.Name{Space:"", Local:"Person"}
// Name: "Grace R. Emlin"
// Phone: "none"
// Email: [{home gre@example.com} {work gre@work.com}]
// Groups: [Friends Squash]
+ // Address: {Hanga Roa Easter Island}
}
diff --git a/src/pkg/encoding/xml/marshal.go b/src/pkg/encoding/xml/marshal.go
index 25d88c4619b2d0d9cbb65b256d90f671ea561e5a..6c3170bdda3d42074b9721b9ac21ebc401e7f82d 100644
--- a/src/pkg/encoding/xml/marshal.go
+++ b/src/pkg/encoding/xml/marshal.go
@@ -57,6 +57,8 @@ // - a field with a tag including the "omitempty" option is omitted
// if the field value is empty. The empty values are false, 0, any
// nil pointer or interface value, and any array, slice, map, or
// string of length zero.
+// - a non-pointer anonymous struct field is handled as if the
+// fields of its value were part of the outer struct.
//
// If a field uses a tag "a>b>c", then the element c will be nested inside
// parent elements a and b. Fields that appear next to each other that name
diff --git a/src/pkg/encoding/xml/read.go b/src/pkg/encoding/xml/read.go
index 7f5601a7ad1f9a365ff25f09e1f7fb901657c648..c2168242091a493f3c7af28e182c6da2aa32048c 100644
--- a/src/pkg/encoding/xml/read.go
+++ b/src/pkg/encoding/xml/read.go
@@ -81,6 +81,9 @@ // * If the XML element contains a sub-element that hasn't matched any
// of the above rules and the struct has a field with tag ",any",
// unmarshal maps the sub-element to that struct field.
//
+// * A non-pointer anonymous struct field is handled as if the
+// fields of its value were part of the outer struct.
+//
// * A struct field with tag "-" is never unmarshalled into.
//
// Unmarshal maps an XML element to a string or []byte by saving the