golang xml解析
发布时间:2020-12-16 19:23:58 所属栏目:大数据 来源:网络整理
导读:golang解析xml真是好用,特别是struct属性的tag让程序简单了许多,其他变成语言需要特殊类型的在golang里直接使用tag舒服 xml文件地址:http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/doc/addresslist.zip http://wiki.opensns.qq.com/wiki/【QQ登录】get_
golang解析xml真是好用,特别是struct属性的tag让程序简单了许多,其他变成语言需要特殊类型的在golang里直接使用tag舒服 xml文件地址:http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/doc/addresslist.zip packagemain import( "os" "encoding/xml" //"encoding/json" "io/ioutil" "fmt" ) typeLocationstruct{ CountryRegion[]CountryRegion } typeCountryRegionstruct{ Namestring`xml:",attr"` Codestring`xml:",attr"` State[]State } typeStatestruct{ Namestring`xml:",attr"` City[]City } typeCitystruct{ Namestring`xml:",attr"` Region[]Region } typeRegionstruct{ Namestring`xml:",attr"` } funcmain(){ f,err:=os.Open("LocList.xml") iferr!=nil{ panic(err) } data,err:=ioutil.ReadAll(f) iferr!=nil{ panic(err) } //v:=make(map[string]interface{}) varvLocation err=xml.Unmarshal(data,&v) iferr!=nil{ panic(err) } //fmt.Printf("%#vn",v) //table for_,countryRegion:=rangev.CountryRegion{ //fmt.Printf("%s,%sn",countryRegion.Code,countryRegion.Name) iflen(countryRegion.State)==0{ continue } for_,state:=rangecountryRegion.State{ //fmt.Printf("%s,%s,state.Code,state.Name) iflen(state.City)==0{ continue } for_,city:=rangestate.City{ //fmt.Printf("%s,city.Code,city.Name) iflen(city.Region)==0{ continue } for_,region:=rangecity.Region{ fmt.Printf("%s,region.Code,region.Name) } } } } ////json //js,err:=json.Marshal(&v.CountryRegion[0]) //iferr!=nil{ //panic(err) //} //fmt.Printf("%sn",js) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |