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