Created
January 15, 2015 13:16
-
-
Save un1t/ac574c298e705044a589 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"os" | |
"flag" | |
"encoding/xml" | |
) | |
type Offer struct { | |
Name string `xml:"name"` | |
} | |
var inputFile = flag.String("infile", "", "Input file path") | |
func main() { | |
flag.Parse() | |
if (*inputFile == "") { | |
fmt.Fprintln(os.Stderr, "Error: missing input file.") | |
return | |
} | |
xmlFile, err := os.Open(*inputFile) | |
if err != nil { | |
fmt.Println("Error opening file:", err) | |
return | |
} | |
defer xmlFile.Close() | |
decoder := xml.NewDecoder(xmlFile) | |
total := 0 | |
var tagName string | |
for { | |
token, _ := decoder.Token() | |
if token == nil { | |
break | |
} | |
switch element := token.(type) { | |
case xml.StartElement: | |
tagName = element.Name.Local | |
if tagName == "offer" { | |
total++ | |
var offer Offer | |
decoder.DecodeElement(&offer, &element) | |
// fmt.Println(offer.Name) | |
} | |
default: | |
} | |
} | |
fmt.Printf("Total: %d \n", total) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment