Golang encoding/xml.Decoder.DecodeElement() function example
package encoding/xml
DecodeElement works like xml.Unmarshal except that it takes a pointer to the start XML element to decode into v. It is useful when a client reads some raw XML tokens itself but also wants to defer to Unmarshal for some elements.
Golang encoding/xml.Decoder.DecodeElement() function usage example
type Rect struct {
Date string `xml:"data-date,attr"`
Num string `xml:"data-count,attr"`
}
func (c *Contribution) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var i Rect
err := d.DecodeElement(&i, &start) // <- here
if err != nil {
return err
}
c.Date, err = time.Parse(dateFormat, i.Date)
if err != nil {
return err
}
c.Num, err = strconv.Atoi(i.Num)
if err != nil {
return err
}
return nil
}
Reference :
Advertisement
Something interesting
Tutorials
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+20.8k Golang : Convert date string to variants of time.Time type examples
+24.5k Golang : Time slice or date sort and reverse sort example
+5.9k Golang : Denco multiplexer example
+17.2k Golang : Find file size(disk usage) with filepath.Walk
+14.6k Golang : Missing Bazaar command
+13.6k Golang : Strings comparison
+7.1k Golang : Get environment variable
+10.1k Golang : Get login name from environment and prompt for password
+7k Golang : constant 20013 overflows byte error message
+7.2k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream