package main import ( "encoding/xml" "fmt" "os" ) type xmldas struct { XMLName xml.Name `xml:"das"` DataPort string `xml:"DataPort,attr"` Desc string `xml:"desc,attr"` Src xmlsource `xml:"source"` Dest xmldestination `xml:"destination"` } type xmlsource struct { Path string `xml:"path,attr"` Param string `xml:"param,attr"` } type xmldestination struct { Path string `xml:"path,attr"` Param string `xml:"param,attr"` } type DataXML struct { XMLName xml.Name `xml:"DataXML"` Head XMLHead `xml:"Head"` Record []XMLRecord `xml:"Record"` } type XMLHead struct { SystemCode string `xml:"SystemCode"` MappingID string `xml:"MappingID"` Type string `xml:"Type"` FarmCode string `xml:"FarmCode"` DataNum string `xml:"DataNum"` } type XMLRecord struct { FeedCode string FeedName string FarmCode string FeedDate string Banci string BarName string GroupTypeCode string GroupTypeName string CowAmount string PCows string RecipeCode string RecipeName string MixBatch string BarStartTime string BarEndTime string BarRecipePlanWeight string BarRecipeRealWeight string BarFeedPlanWeight string BarFeedRealWeight string FDate string DStatus string UserName string UserTrueName string } func main() { v := xmldas{DataPort: "8250", Desc: "123"} v.Src = xmlsource{Path: "123", Param: "456"} v.Dest = xmldestination{Path: "789", Param: "000"} output, err := xml.MarshalIndent(v, " ", " ") if err != nil { fmt.Printf("error: %v\n", err) } os.Stdout.Write([]byte(xml.Header)) os.Stdout.Write(output) W := DataXML{} W.Head = XMLHead{ SystemCode : "EE", MappingID : "RR", Type : "TT", FarmCode : "YY", DataNum : "UU", } re := XMLRecord{ FeedCode : "xx", FeedName : "cc", FarmCode : "vv", FeedDate : "jj", } re1 := XMLRecord{ FeedCode : "qq", FeedName : "tt", FarmCode : "vv", FeedDate : "jj", } W.Record = append(W.Record,re,re1) output1, _ := xml.MarshalIndent(W, " ", " ") fmt.Println(string(output1)) }