-
-
Save blide/4f49b774c3cbb24e3116 to your computer and use it in GitHub Desktop.
xml
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 ( | |
| "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