How to parse huge XML file with various elements in Go?
Use the standard xml Decoder.
Call Token to read tokens one by one. When a start element of interest is found, call DecodeElement to decode the element to a Go value.
Here's a sketch of how to use the decoder:
d := xml.NewDecoder(r)
for {
t, tokenErr := d.Token()
if tokenErr != nil {
if tokenErr == io.EOF {
break
}
// handle error
}
switch t := t.(type) {
case xml.StartElement:
if t.Name.Space == "foo" && t.Name.Local == "bar" {
var b bar
if err := d.DecodeElement(&b, &t); err != nil {
// handle error
}
// do something with b
}
}
}