英文:
Best way to construct XML from JSON in Golang
问题
我有一个中间件,我在其中接收JSON输入和用户凭据,并需要提取它们以构建包含其他各种数据的完整XML。
假设我有以下代码来解码JSON:
json.NewDecoder(r.Request.Body).Decode(entityPointer)
从这里构建XML的最有效方法是什么?
我想我可以通过匹配结构体并使用它们,或者使用现有的XML模板解析它们并替换模板变量来实现。
例如,如果我有一个请求{username: '11', password: 'pass'}
,我该如何构建以下XML:
英文:
I have a middleware where I receive JSON input and with user credentials and needs to grab them to construct a full XML with various other data.
Suppose I have below code to decode JSON:
json.NewDecoder(r.Request.Body).Decode(entityPointer)
What is the most efficient way to construct XML from here?
I thought I could just match with struct and use them or parse them with existing XML template and replace the template variables?
if I had for example {username: '11', password: 'pass'}
as request, How can I construct below XML out of
答案1
得分: 1
你可以在XML和JSON中使用相同的结构体,例如:
type Person struct {
Id int `xml:"id,attr"`
FirstName string `xml:"name>first" json:"first"`
LastName string `xml:"name>last" json:"last"`
}
func main() {
j := `{"id": 10, "first": "firstname", "last":"lastname"}`
var p Person
fmt.Println(json.Unmarshal([]byte(j), &p), p)
out, _ := xml.MarshalIndent(p, "\t", "\t")
fmt.Println(string(out))
}
在http://golang.org/pkg/encoding/xml/#example_Encoder中查看XML示例。
//编辑
嗯,既然你已经有一个模板,你可以使用html/template
,例如:
const xmlTmpl = `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE api SYSTEM "api.dtd">
<api version="6.0">
<request>
<reqClient returnToken="N">
<user>{{.AdminUsername}} </user>
<password>{{.AdminPassword}}</password>
</reqClient>
<reqValidate returnBalance="Y">
<userName>{{.Username}}</userName>
<password>{{.Password}}</password>
<channel>M</channel>
</reqValidate>
</request>
</api>
`
var tmpl = template.Must(template.New("foo").Parse(xmlTmpl))
type Entity struct {
AdminUsername string `json:"-"`
AdminPassword string `json:"-"`
Username, Password string
}
func main() {
e := Entity{Username: "User", Password: "Loser"}
//json.NewDecoder(r.Request.Body).Decode(&e)
e.AdminUsername = "admin" // 在解析请求后填充管理员用户名/密码
e.AdminPassword = "admin-password"
fmt.Println(tmpl.Execute(os.Stdout, e))
}
英文:
You can use the same struct for both XML and JSON, for example:
type Person struct {
Id int `xml:"id,attr"`
FirstName string `xml:"name>first" json:"first"`
LastName string `xml:"name>last" json:"last"`
}
func main() {
j := `{"id": 10, "first": "firstname", "last":"lastname"}`
var p Person
fmt.Println(json.Unmarshal([]byte(j), &p), p)
out, _ := xml.MarshalIndent(p, "\t", "\t")
fmt.Println(string(out))
}
Check the xml examples @ http://golang.org/pkg/encoding/xml/#example_Encoder
//edit
Well, since you already have a template you could use html/template
, for example:
const xmlTmpl = `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE api SYSTEM "api.dtd">
<api version="6.0">
<request>
<reqClient returnToken="N">
<user>{{.AdminUsername}} </user>
<password>{{.AdminPassword}}</password>
</reqClient><reqValidate returnBalance="Y">
<userName>{{.Username}}</userName>
<password>{{.Password}}</password>
<channel>M</channel>
</reqValidate></request>
</api>
`
var tmpl = template.Must(template.New("foo").Parse(xmlTmpl))
type Entity struct {
AdminUsername string `json:"-"`
AdminPassword string `json:"-"`
Username, Password string
}
func main() {
e := Entity{Username: "User", Password: "Loser"}
//json.NewDecoder(r.Request.Body).Decode(&e)
e.AdminUsername = "admin" // fill admin user/pass after parsing the request
e.AdminPassword = "admin-password"
fmt.Println(tmpl.Execute(os.Stdout, e))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论