Golang encoding/xml.Decoder.Decode() function example
package encoding/xml
Decode works like xml.Unmarshal, except it reads the decoder stream to find the start element.
Golang encoding/xml.Decoder.Decode() function usage example
type xmlRegistry struct {
Types []xmlType `xml:"types>type"`
Enums []xmlEnumSet `xml:"enums"`
Commands []xmlCommand `xml:"commands>command"`
Features []xmlFeature `xml:"feature"`
Extensions []xmlExtension `xml:"extensions>extension"`
}
func readSpecFile(file string) (*xmlRegistry, error) {
var registry xmlRegistry
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
err = xml.NewDecoder(f).Decode(®istry) // <----- here
if err != nil {
return nil, err
}
return ®istry, nil
}
Reference :
Advertisement
Something interesting
Tutorials
+25.7k Golang : How to write CSV data to file
+16.3k Golang : convert string or integer to big.Int type
+35.5k Golang : Smarter Error Handling with strings.Contains()
+26.8k Golang : Convert file content into array of bytes
+16k Golang : Get sub string example
+20.2k Golang : How to get struct tag and use field name to retrieve data?
+21.1k Golang : Sort and reverse sort a slice of strings
+9.9k Golang : Translate language with language package example
+6.8k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+8.8k Golang : Executing and evaluating nested loop in html template
+5.9k Golang : Detect variable or constant type
+10.6k Golang : Allow Cross-Origin Resource Sharing request