golang xml解析

                                                                                                     

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_info

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) }



本文来自:开源中国博客

感谢作者:____追梦人

查看原文:golang xml解析




查看原文:http://www.zoues.com/2016/10/20/golang-xml%e8%a7%a3%e6%9e%90/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值