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
+11.6k Golang : Concurrency and goroutine example
+9.7k Random number generation with crypto/rand in Go
+14.6k Golang : Convert(cast) int to float example
+15.3k nginx: [emerg] unknown directive "ssl"
+11.6k Golang : Display a text file line by line with line number example
+11.6k Golang : Surveillance with web camera and OpenCV
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+22.8k Golang : untar or extract tar ball archive example
+10.2k Golang : Bcrypting password
+10.1k Golang : How to get quoted string into another string?
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+9.1k Golang : How to capture return values from goroutines?