咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

golang xml 处理之生成 属性必须大写

Posted on 2013-08-16 15:53 oathleo 阅读(2675) 评论(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)
}

Feedback

# re: golang xml 处理之生成 属性必须大写  回复  更多评论   

2013-12-15 00:00 by 左岸
学习了,留以备用

只有注册用户登录后才能发表评论。


网站导航: