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.

Revisions

  1. blide created this gist Jul 30, 2015.
    49 changes: 49 additions & 0 deletions .go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    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))
    }
    }
    }
    }