Skip to content

Instantly share code, notes, and snippets.

@blide
Created July 30, 2015 11:42
Show Gist options
  • Select an option

  • Save blide/4f49b774c3cbb24e3116 to your computer and use it in GitHub Desktop.

Select an option

Save blide/4f49b774c3cbb24e3116 to your computer and use it in GitHub Desktop.
xml
package main
import (
"encoding/xml"
"fmt"
"os"
"runtime"
)
type DrugStores struct {
XMLName xml.Name `xml:"DrugStores"`
Stores []DrugStore `xml:"DrugStore"`
}
type DrugStore struct {
XMLName xml.Name `xml:"DrugStore`
AgentName string `xml:"agentName"`
Items []Item `xml:"Item"`
}
type Item struct {
XMLName xml.Name `xml:"Item"`
ItemName string `xml:"itemName"`
}
var c = make(chan int, 5)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
xmlFile, err := os.Open("goods.xml")
if err != nil {
return
}
stores := DrugStores{}
decoder := xml.NewDecoder(xmlFile)
for {
t, _ := decoder.Token()
switch se := t.(type) {
case xml.StartElement:
if se.Name.Local == "DrugStore" {
var store DrugStore
decoder.DecodeElement(&store, &se)
fmt.Println(len(store.Items))
stores.Stores = append(stores.Stores, store)
fmt.Println(len(stores.Stores))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment