| // Copyright 2018 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| func (a *Animal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
| if err := d.DecodeElement(&s, &start); err != nil { |
| switch strings.ToLower(s) { |
| func (a Animal) MarshalXML(e *xml.Encoder, start xml.StartElement) error { |
| return e.EncodeElement(s, start) |
| func Example_customMarshalXML() { |
| <animal>armadillo</animal> |
| Animals []Animal `xml:"animal"` |
| if err := xml.Unmarshal([]byte(blob), &zoo); err != nil { |
| census := make(map[Animal]int) |
| for _, animal := range zoo.Animals { |
| fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n", |
| census[Gopher], census[Zebra], census[Unknown]) |