golang解析xml的方法
更新時間:2016年07月22日 11:38:50 作者:dotcoo
這篇文章主要介紹了golang解析xml的方法,結(jié)合實例形式分析了Go語言針對xml文件的讀取與解析的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了golang解析xml的方法。分享給大家供大家參考,具體如下:
golang解析xml真是好用,特別是struct屬性的tag讓程序簡單了許多,其他變成語言需要特殊類型的在golang里直接使用tag舒服
xml文件點擊此處本站下載。
完整示例代碼:
復(fù)制代碼 代碼如下:
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)
}
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語言程序設(shè)計有所幫助。
相關(guān)文章
使用Go和Gorm實現(xiàn)讀取SQLCipher加密數(shù)據(jù)庫
本文檔主要描述通過Go和Gorm實現(xiàn)生成和讀取SQLCipher加密數(shù)據(jù)庫以及其中踩的一些坑,文章通過代碼示例講解的非常詳細, 對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06基于go+vue實現(xiàn)的golang每日新聞數(shù)據(jù)瀏覽與檢索平臺(推薦)
gonews是基于 go+vue 實現(xiàn)的golang每日新聞瀏覽與檢索平臺,本文通過實例代碼給大家講解,介紹的非常詳細,具有參考借鑒價值,需要的朋友參考下吧2018-01-01