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.7k Golang : Calculations using complex numbers example
+11.8k Golang : GTK Input dialog box examples
+8.1k Golang : Variadic function arguments sanity check example
+10.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+11.9k Golang : How to parse plain email text and process email header?
+39.2k Golang : How to read CSV file
+7.1k Golang : A simple forex opportunities scanner
+17k Golang : Get number of CPU cores
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+80.7k Golang : How to return HTTP status code?
+7.5k Golang : How to stop user from directly running an executable file?
+15.3k Golang : Get all local users and print out their home directory, description and group id