Posted on 2013-08-16 15:53 
oathleo 阅读(2706) 
评论(1)  编辑  收藏  所属分类: 
Golang 
			 
			
		 
		xml对应的struct 属性必须大写,否则无法实现!
Code是必须的
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"`
}
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)
}