go生成xml

package main

import (
	"encoding/xml"
	"fmt"
	// "os"
)

type Servers struct {
	XMLName xml.Name `xml:"servers"`
	Version string   `xml:"version,attr"`
	Svs     []server `xml:"server"`
}

type server struct {
	ServerName string `xml:"serverName"`
	ServerIP   string `xml:"serverIP"`
}

func main() {
	v := &Servers{Version: "1"}
	v.Svs = append(v.Svs, server{"Shanghai_VPN", "127.0.0.1"})
	v.Svs = append(v.Svs, server{"Beijing_VPN", "127.0.0.2"})
	output, err := xml.MarshalIndent(v, "  ", "    ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}
	// os.Stdout.Write([]byte(xml.Header))

	// os.Stdout.Write(output)
	//将字节流转换成string输出
	fmt.Println(string(output))
}
posted @ 2016-11-13 17:59  郑闯  阅读(1903)  评论(0编辑  收藏  举报