Skip to content

Instantly share code, notes, and snippets.

@un1t
Created January 15, 2015 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save un1t/ac574c298e705044a589 to your computer and use it in GitHub Desktop.
Save un1t/ac574c298e705044a589 to your computer and use it in GitHub Desktop.
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