Golang解析xml文件
一段xml数据:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
<?xml version="1.0" encoding="utf-8"?>
<data>
<head>
<service name="query_syj_cyfwxk" />
</head>
<body type="out">
<result count="38">
<row XuKeZhengHao="" XinDanWeiMingC="" XinJingYingDiZ="" XinXingMing="" ShenQingXiangMu="" ShenQingXiangMuBz="" XinLianXiDianH="" XinJingJiXingZ="" SuoShuXiaQu="" XinCongYeRenYS="" JiaGongJingYingCSMJZJ="" ZhengZhaoYouXiaoQSRQ="" ZhengZhaoYouXiaoJZRQ="" LeiXing="新发" SuoShuXiaQu="" />
<row XuKeZhengHao="" XinDanWeiMingC="" XinJingYingDiZ="" XinXingMing="" ShenQingXiangMu="" ShenQingXiangMuBz="" XinLianXiDianH="" XinJingJiXingZ="" SuoShuXiaQu="" XinCongYeRenYS="" JiaGongJingYingCSMJZJ="" ZhengZhaoYouXiaoQSRQ="" ZhengZhaoYouXiaoJZRQ="" LeiXing="新发" SuoShuXiaQu="" />
<row XuKeZhengHao="" XinDanWeiMingC="延期测试2" XinJingYingDiZ="延期测试2" XinXingMing="延期测试2" ShenQingXiangMu="" ShenQingXiangMuBz="" XinLianXiDianH="" XinJingJiXingZ="延期测试2" SuoShuXiaQu="" XinCongYeRenYS="2" JiaGongJingYingCSMJZJ="" ZhengZhaoYouXiaoQSRQ="" ZhengZhaoYouXiaoJZRQ="" LeiXing="新发" SuoShuXiaQu="" />
</result>
</body>
</data>
</string>
Go代码:
package webService
import (
"bufio"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
type GetWebServiceDataController struct {
BaseController
}
var (
//获取数据地址
url = ""
//保存获取数据的服务器地址
XmlPath = "/static/xml"
)
//xml中的string标签的结构体
type String struct {
String string `xml:"xmlns,attr"`
Xml []Xml `xml:"?xml"`
Data []Data `xml:"data"`
}
//xml中的Xml标签的结构体
type Xml struct {
Version string `xml:"version,attr"`
Encoding string `xml:"encoding,attr"`
}
//xml中的data标签的结构体
type Data struct {
Head []Head `xml:"head"`
Body []Body `xml:"body"`
}
//xml中的head标签的结构体
type Head struct {
Service []Service `xml:",attr"`
}
//xml中的service标签的结构体
type Service struct {
Name string `xml:"name,attr"`
}
//xml中的body标签的结构体
type Body struct {
Type string `xml:"type,attr"`
Results []Results `xml:"result"`
}
//xml中的result标签的结构体
type Results struct {
Count string `xml:"count,attr"`
Row []Row `xml:"row"`
}
//xml中的result标签的结构体
type Result struct {
Count string `xml:"count,attr"`
}
//xml中的row标签的结构体
type Row struct {
XuKeZhengHao string `xml:"XuKeZhengHao,attr"`
XinDanWeiMingC string `xml:"XinDanWeiMingC,attr"`
XinJingYingDiZ string `xml:"XinJingYingDiZ,attr"`
XinXingMing string `xml:"XinXingMing,attr"`
ShenQingXiangMu string `xml:"ShenQingXiangMu,attr"`
ShenQingXiangMuBz string `xml:"ShenQingXiangMuBz,attr"`
XinLianXiDianH string `xml:"XinLianXiDianH,attr"`
XinJingJiXingZ string `xml:"XinJingJiXingZ,attr"`
SuoShuXiaQu string `xml:"SuoShuXiaQu,attr"`
XinCongYeRenYS string `xml:"XinCongYeRenYS,attr"`
JiaGongJingYingCSMJZJ string `xml:"JiaGongJingYingCSMJZJ,attr"`
ZhengZhaoYouXiaoQSRQ string `xml:"ZhengZhaoYouXiaoQSRQ,attr"`
ZhengZhaoYouXiaoJZRQ string `xml:"ZhengZhaoYouXiaoJZRQ,attr"`
LeiXing string `xml:"LeiXing,attr"`
}
//解析保存到本地的xml数据,并存到数据库中
func (this *GetWebServiceDataController) GetWebServiceData() {
fileName := this.WriteDataToXml()
f, err := os.Open(fileName)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
//fmt.Println("xml中的数据:=\n", string(data))
var v String
err = xml.Unmarshal(data, &v)
if err != nil {
panic(err)
}
//fmt.Println("aaaa=", v.Data)
for _, data := range v.Data {
for _, body := range data.Body {
for _, result := range body.Results {
for k, row := range result.Row {
svc := webService.GetWebService(utils.DBE)
var entity afscateringlicense.AFS_CateringLicense
b := svc.GetIsExistByLicense(row.XuKeZhengHao, &entity)
if b == false {
//fmt.Println(k, row.XuKeZhengHao, row.XinDanWeiMingC)
this.ParseForm(&entity)
entity.License = row.XuKeZhengHao
entity.UnitName = row.XinDanWeiMingC
entity.Address = row.XinJingYingDiZ
entity.LegalPerson = row.XinXingMing
entity.Type = row.ShenQingXiangMu
entity.Memo = row.ShenQingXiangMuBz
entity.Tel = row.XinLianXiDianH
entity.Nature = row.XinJingJiXingZ
entity.Regional = row.SuoShuXiaQu
entity.PractitionersNumber = row.XinCongYeRenYS
entity.Area = row.JiaGongJingYingCSMJZJ
entity.PeriodValidityBegin, _ = time.Parse(row.ZhengZhaoYouXiaoQSRQ, "2006-01-02")
entity.PeriodValidityEnd, _ = time.Parse(row.ZhengZhaoYouXiaoJZRQ, "2006-01-02")
entity.NewCardType = row.LeiXing
_, err := svc.InsertWebServiceData(entity)
if err == nil {
this.Ctx.WriteString("添加成功")
} else {
this.Ctx.WriteString("添加失败")
}
} else {
_, err := svc.UpdateEntityByLicense(row.XuKeZhengHao, &entity)
if err == nil {
this.Ctx.WriteString("修改成功")
} else {
this.Ctx.WriteString("修改失败")
}
}
}
}
}
}
}
//把获取到的数据保存到服务器/static/xml文件夹下
func (this *GetWebServiceDataController) WriteDataToXml() string {
var s string
body := this.Apiget(url)
s = string(body)
s = strings.Replace(s, "<", "<", -1)
s = strings.Replace(s, ">", ">", -1)
var spit string
if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符
spit = "\\"
} else {
spit = "/"
}
var urlpath = XmlPath
dir, _ := os.Getwd() //当前目录
path := dir + spit + strings.Replace(XmlPath, "/", spit, -1) + spit + utils.ToStr(time.Now().Year()) + spit + fmt.Sprintf("%s", time.Now().Format("1")) + spit + "Cateringlicense" + spit
os.MkdirAll(path, os.ModePerm)
var fileName = "Cateringlicense-" + time.Now().Format("2006-01-02-15-04") + ".xml"
f, _ := os.Create(path + fileName)
defer f.Close()
writer := bufio.NewWriter(f)
writer.WriteString(s)
writer.WriteString("\n")
writer.Flush()
return dir + "/" + urlpath + "/" + utils.ToStr(time.Now().Year()) + "/" + fmt.Sprintf("%s", time.Now().Format("1")) + "/" + "Cateringlicense" + "/" + fileName
}
//通过Get方式获取接口餐饮服务的数据
func (this *GetWebServiceDataController) Apiget(str string) (body []byte) {
response, _ := http.Get(str)
if response != nil {
defer response.Body.Close()
body, _ = ioutil.ReadAll(response.Body)
}
return
}
操作数据库代码:
package webService
import (
"github.com/go-xorm/xorm"
)
type WebService struct {
ServiceBase
}
func GetWebService(xormEngine *xorm.Engine) *WebService {
s := new(WebService)
s.DBE = xormEngine
return s
}
//把xml中的数据插入到数据库中
func (s WebService) InsertWebServiceData(entity interface{}) (affected int64, err error) {
affected, err = s.DBE.Insert(entity)
LogError(err)
return
}
//根据证号查询是否有重复
func (s *WebService) GetIsExistByLicense(License string, entity interface{}) bool {
b, _ := utils.DBE.Where("License = '" + License + "'").Get(entity)
return b
}
//如果有重复的数据,根据证号进行修改
func (s *WebService) UpdateEntityByLicense(License string, entity interface{}) (affected int64, err error) {
affected, err = s.DBE.Where("License = '" + License + "'").Update(entity)
LogError(err)
return
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。