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
+10.6k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+8.2k Golang : HttpRouter multiplexer routing example
+6.3k Golang : Extract sub-strings
+12.3k Golang : How to check if a string starts or ends with certain characters or words?
+14.2k Elastic Search : Mapping date format and sort by date
+20.7k Golang : Read directory content with os.Open
+8.2k Android Studio : Rating bar example
+9.6k Golang : Copy map(hash table) example
+12.1k Golang : Save webcamera frames to video file
+22.1k Golang : Repeat a character by multiple of x factor
+44.9k Golang : Use wildcard patterns with filepath.Glob() example
+20.7k Golang : Saving private and public key to files