golang中如何解析xml,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元那坡做網(wǎng)站,已為上家服務(wù),為那坡各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
golang解析xml真是好用,特別是struct屬性的tag讓程序簡單了許多,其他變成語言需要特殊類型的在golang里直接使用tag舒服
完整示例代碼:
package main import ( "os" "encoding/xml" // "encoding/json" "io/ioutil" "fmt" ) type Location struct { CountryRegion []CountryRegion } type CountryRegion struct { Name string `xml:",attr"` Code string `xml:",attr"` State []State } type State struct { Name string `xml:",attr"` Code string `xml:",attr"` City []City } type City struct { Name string `xml:",attr"` Code string `xml:",attr"` Region []Region } type Region struct { Name string `xml:",attr"` Code string `xml:",attr"` } func main() { f, err := os.Open("LocList.xml") if err != nil { panic(err) } data, err := ioutil.ReadAll(f) if err != nil { panic(err) } // v := make(map[string]interface{}) var v Location err = xml.Unmarshal(data, &v) if err != nil { panic(err) } // fmt.Printf("%#v\n", v) // table for _, countryRegion := range v.CountryRegion { // fmt.Printf("%s,%s\n", countryRegion.Code, countryRegion.Name) if len(countryRegion.State) == 0 { continue } for _, state := range countryRegion.State { // fmt.Printf("%s,%s,%s\n", countryRegion.Code, state.Code, state.Name) if len(state.City) == 0 { continue } for _, city := range state.City { // fmt.Printf("%s,%s,%s,%s\n", countryRegion.Code, state.Code, city.Code, city.Name) if len(city.Region) == 0 { continue } for _, region := range city.Region { fmt.Printf("%s,%s,%s,%s,%s\n", countryRegion.Code, state.Code, city.Code, region.Code, region.Name) } } } } // // json // js, err := json.Marshal(&v.CountryRegion[0]) // if err != nil { // panic(err) // } // fmt.Printf("%s\n", js) }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)站題目:golang中如何解析xml
當前網(wǎng)址:http://muchs.cn/article26/ippdjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、用戶體驗、做網(wǎng)站、網(wǎng)站維護、響應(yīng)式網(wǎng)站、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)