This is a creation in Article, where the information may have evolved or changed.
A section of XML data:
<?xml version= "1.0" encoding= "Utf-8"?>
<string xmlns= "http://tempuri.org/" >
<?xml version= "1.0" encoding= "Utf-8"?>
<data>
<service name= "Query_syj_cyfwxk"/>
<body type= "Out" >
<result count= ">"
<row xukezhenghao= "" xindanweimingc= "" xinjingyingdiz= "" xinxingming= "" shenqingxiangmu= "" ShenQingXiangMuBz= "" Xinlianxidianh= "xinjingjixingz=" "suoshuxiaqu=" "xincongyerenys=" "jiagongjingyingcsmjzj=" "ZhengZhaoYouXiaoQSRQ= "Zhengzhaoyouxiaojzrq=" "leixing=" new "suoshuxiaqu=" "/>
<row xukezhenghao= "" xindanweimingc= "" xinjingyingdiz= "" xinxingming= "" shenqingxiangmu= "" ShenQingXiangMuBz= "" Xinlianxidianh= "xinjingjixingz=" "suoshuxiaqu=" "xincongyerenys=" "jiagongjingyingcsmjzj=" "ZhengZhaoYouXiaoQSRQ= "Zhengzhaoyouxiaojzrq=" "leixing=" new "suoshuxiaqu=" "/>
<row xukezhenghao= "xindanweimingc=" delay Test 2 "xinjingyingdiz=" Delay Test 2 "xinxingming=" Delay Test 2 "shenqingxiangmu=" " shenqingxiangmubz= "" Xinlianxidianh= "" xinjingjixingz= "delay Test 2" suoshuxiaqu= "" xincongyerenys= "2" jiagongjingyingcsmjzj= "zhengzhaoyouxiaoqsrq=" "zhengzhaoyouxiaojzrq=" "leixing=" New Hair "suoshuxiaqu=" "/>
</result>
</body>
</data>
</string>
Go code:
Package WebService
Import (
"Bufio"
"Encoding/xml"
"FMT"
"Io/ioutil"
"Net/http"
"OS"
"Strings"
"Time"
)
Type Getwebservicedatacontroller struct {
Basecontroller
}
VAR (
Get Data address
url = ""
Save the server address to get the data
Xmlpath = "/static/xml"
)
Structure of a string tag in XML
Type String struct {
String string ' xml: ' xmlns,attr '
XML []xml ' xml: '? xml '
data []data ' xml: ' Data '
}
The structure of XML tags in XML
Type Xml struct {
Version string ' xml: ' version,attr '
Encoding string ' xml: ' encoding,attr '
}
The structure of the data tag in XML
Type Data struct {
Head []head ' xml: "Head" '
Body []body ' xml: "Body" '
}
The structure of the head tag in XML
Type Head struct {
Service []service ' xml: ", attr" '
}
The structure of the service tag in XML
Type Service struct {
Name string ' xml: ' name,attr '
}
Structure of body tag in XML
Type Body struct {
Type string ' xml: ' type,attr '
Results []results ' xml: ' result '
}
The structure of the result tag in XML
Type Results struct {
Count string ' xml: ' count,attr '
row []row ' xml: ' Row '
}
The structure of the result tag in XML
Type Result struct {
Count string ' xml: ' count,attr '
}
The structure of the row tag in XML
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 '
}
Parse the XML data saved to the local and coexist in the database
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 ("Data in XML: =\n", String)
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 ("Add Success")
} else {
This. Ctx.writestring ("Add Failed")
}
} else {
_, Err: = svc. Updateentitybylicense (row. Xukezhenghao, &entity)
If Err = = Nil {
This. Ctx.writestring ("modified successfully")
} else {
This. Ctx.writestring ("Modify failed")
}
}
}
}
}
}
}
Save the acquired data to the server/static/xml folder
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 (' \ \ ') {//Front of the judgment is the system delimiter
spit = "\ \"
} else {
Spit = "/"
}
var URLPath = Xmlpath
Dir, _: = OS. GETWD ()//current directory
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 the data for the interface catering service via get method
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
}
Manipulating Database code:
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
}
Inserting data from XML into a database
Func (S WebService) Insertwebservicedata (Entity interface{}) (affected int64, err error) {
affected, err = s.dbe. Insert (Entity)
LogError (ERR)
Return
}
Query for duplicates based on number of certificates
Func (S *webservice) getisexistbylicense (License String, entity interface{}) bool {
B, _: = Utils. DBE. Where ("License = '" + License + "'"). Get (Entity)
Return b
}
If there is duplicate data, modify it according to the license number
Func (S *webservice) updateentitybylicense (License String, Entity interface{}) (affected int64, err error) {
affected, err = s.dbe. Where ("License = '" + License + "'"). Update (Entity)
LogError (ERR)
Return
}