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
+36k Golang : Get file last modified date and time
+11.7k Golang : Find age or leap age from date of birth example
+12.3k Golang : Get month name from date example
+15.6k Golang : How to convert(cast) IP address to string?
+11.5k CodeIgniter : Import Linkedin data
+8.8k Golang : Executing and evaluating nested loop in html template
+10.4k Golang : Generate random integer or float number
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+7.4k Golang : Convert source code to assembly language
+10.7k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+17.8k Golang : Iterate linked list example
+8.9k Golang : Find network service name from given port and protocol