Created
January 25, 2018 19:55
-
-
Save mkock/2ad3b4c647fe592b3025ea3ae59a4265 to your computer and use it in GitHub Desktop.
Go implemenation of DMR parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "strings" | |
| ) | |
| // StringParser parses XML as strings. | |
| type StringParser struct{} | |
| // NewStringParser returns a DMR string parser. | |
| func NewStringParser() *StringParser { | |
| return &StringParser{} | |
| } | |
| func getXMLVal(line string) string { | |
| start := strings.Index(line, ">") + 1 | |
| end := strings.LastIndex(line, "<") | |
| return line[start:end] | |
| } | |
| // ParseFile runs the string parser. | |
| func (p *StringParser) ParseFile(file *os.File) (*VehicleList, error) { | |
| var vehicles VehicleList = make(map[string]struct{}) | |
| var isCar bool | |
| csv, brand, model := "", "", "" | |
| scanner := bufio.NewScanner(file) | |
| for scanner.Scan() { | |
| line := strings.TrimSpace(scanner.Text()) | |
| if strings.HasPrefix(line, "<ns:KoeretoejArtNummer>") { | |
| isCar = strings.HasPrefix(line, "<ns:KoeretoejArtNummer>1<") | |
| continue | |
| } | |
| if isCar { | |
| if strings.HasPrefix(line, "<ns:KoeretoejMaerkeTypeNavn>") { | |
| brand = getXMLVal(line) | |
| } else if strings.HasPrefix(line, "<ns:KoeretoejModelTypeNavn>") { | |
| model = getXMLVal(line) | |
| } | |
| if brand != "" && model != "" { | |
| csv = fmt.Sprintf("%v;%v", brand, model) | |
| if _, ok := vehicles[csv]; !ok { | |
| vehicles[csv] = struct{}{} | |
| } | |
| brand, model = "", "" | |
| } | |
| } | |
| } | |
| return &vehicles, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment