• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    golang解析xml的方法
    POST TIME:2021-10-18 17:43

    本文实例讲述了golang解析xml的方法。分享给大家供大家参考,具体如下:

    golang解析xml真是好用,特别是struct属性的tag让程序简单了许多,其他变成语言需要特殊类型的在golang里直接使用tag舒服

    xml文件点击此处本站下载。

    完整示例代码:

    复制代码 代码如下:
    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)
    }

    希望本文所述对大家Go语言程序设计有所帮助。

    您可能感兴趣的文章:
    • golang使用正则表达式解析网页
    • golang中interface接口的深度解析
    • 利用Golang解析json数据的方法示例
    • golang解析网页利器goquery的使用方法
    • golang解析域名的步骤全纪录
    上一篇:golang简单获取上传文件大小的实现代码
    下一篇:golang简单位运算示例
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信