Skip to content

Instantly share code, notes, and snippets.

@mkock
Last active April 27, 2018 12:29
Show Gist options
  • Save mkock/a1dfae4881ee54df82c53d391d713264 to your computer and use it in GitHub Desktop.
Save mkock/a1dfae4881ee54df82c53d391d713264 to your computer and use it in GitHub Desktop.
Concurrent version of the DMR XML parser in Go.
// Same data structures as before.
// ParseExcerpt parses XML file using XML decoding.
func (p *XMLParser) ParseExcerpt(id int, lines <-chan []string, parsed chan<- string, done chan<- int) {
proc := 0 // How many excerpts did we process?
var stat vehicleStat
for excerpt := range lines {
if err := xml.Unmarshal([]byte(strings.Join(excerpt, "\n")), &stat); err != nil {
panic(err) // We _could_ skip it, but it's better to halt execution here.
}
if stat.Type == 1 {
csv := fmt.Sprintf("%v;%v", stat.Info.Designation.BrandTypeName, stat.Info.Designation.Model.Name)
parsed <- csv
proc++
}
}
fmt.Printf("XML-worker %d finished after processing %d excerpts\n", id, proc)
done <- id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment