Golang Go语言中请教一个 XML 序列化问题
我要生成如下 xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<svc_init ver="2.0.0">
<sms ver="2.0.0">
<client>
<id>id</id>
<pwd>pwd</pwd>
<serviceid>serviceid</serviceid>
</client>
<sms_info>
<phone>13013001300</phone>
<content>测试</content>
</sms_info>
</sms>
</svc_init>
代码为
package main
import (
“encoding/xml”
“fmt”
“os”
)
type Client struct {
Id string xml:"client>id"
Pwd string xml:"client>pwd"
Serviceid string xml:"client>serviceid"
}
type Sms_info struct {
Phone string xml:"sms_info>phone"
Content string xml:"sms_info>content"
}
type Sms struct {
XMLName xml.Name xml:"sms"
Ver string xml:"ver,attr"
Client
Sms_info
}
type Svc_init struct {
XMLName xml.Name xml:"svc_init"
Ver string xml:"ver,attr"
Sms
}
func main() {
id := “id”
pwd := “pwd”
serviceid := “serviceid”
client := Client{Id: id, Pwd: pwd, Serviceid: serviceid}
sms_info := Sms_info{Phone: "13013001300", Content: "测试"}
sms := Sms{Ver: "2.0.0", Client: client, Sms_info: sms_info}
v := Svc_init{Ver: "2.0.0", Sms: sms}
fmt.Println(v)
output, _ := xml.MarshalIndent(v, " ", " ")
os.Stdout.Write(output)
}
运行后生成
<svc_init ver="2.0.0">
<client>
<id>id</id>
<pwd>pwd</pwd>
<serviceid>serviceid</serviceid>
</client>
<sms_info>
<phone>13013001300</phone>
<content>测试</content>
</sms_info>
</svc_init>
可以看到结果的 sms 结构丢失了 但是我直接打印 v 值
{{ } 2.0.0 {{ } 2.0.0 {id pwd serviceid} {13013001300 测试}}}
可以看到 sms 结构还在 请问为什么会这样,然后如何修复呢,感谢。
Golang Go语言中请教一个 XML 序列化问题
更多关于Golang Go语言中请教一个 XML 序列化问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
没用过 Go 操作 xml ,但我看你的 Client 、Sms_info 是内嵌的,考虑一下使用 https://www.onlinetool.io/xmltogo/ 生成的结构?
另外打印的时候可以用 fmt.Printf("%+v", v),这样有字段名,好理解一点
更多关于Golang Go语言中请教一个 XML 序列化问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
结构里 代码写写全,IDE 没报错?
感谢,通过页面生成的结构成功了
go<br>package main<br><br>import (<br> "encoding/xml"<br> "os"<br>)<br><br>var data = `<svc_init ver="2.0.0"><br><sms ver="2.0.0"><br> <client><br> <id>id</id><br> <pwd>pwd</pwd><br> <serviceid>serviceid</serviceid><br> </client><br> <sms_info><br> <phone>13013001300</phone><br> <content>测试</content><br> </sms_info><br></sms><br></svc_init>`<br><br>type Client struct {<br> Id string `xml:"client>id"`<br> Pwd string `xml:"client>pwd"`<br> Serviceid string `xml:"client>serviceid"`<br>}<br><br>type Sms_info struct {<br> Phone string `xml:"sms_info>phone"`<br> Content string `xml:"sms_info>content"`<br>}<br><br>type Sms struct {<br> Ver string `xml:"ver,attr"`<br> Client<br> Sms_info<br>}<br><br>type Svc_init struct {<br> XMLName <a target="_blank" href="http://xml.Name" rel="nofollow noopener">xml.Name</a> `xml:"svc_init"`<br> Ver string `xml:"ver,attr"`<br> Sms Sms `xml:"sms"`<br>}<br><br>func main() {<br> id := "id"<br> pwd := "pwd"<br> serviceid := "serviceid"<br><br> client := Client{Id: id, Pwd: pwd, Serviceid: serviceid}<br> sms_info := Sms_info{Phone: "13013001300", Content: "测试"}<br><br> sms := Sms{Ver: "2.0.0", Client: client, Sms_info: sms_info}<br><br> v := Svc_init{Ver: "2.0.0", Sms: sms}<br><br> output, _ := xml.MarshalIndent(v, " ", " ")<br><br> os.Stdout.Write(output)<br>}<br>
给 Sms 添加了一个 xml:"sms"
就好了xml<br><svc_init ver="2.0.0"><br> <sms ver="2.0.0"><br> <client><br> <id>id</id><br> <pwd>pwd</pwd><br> <serviceid>serviceid</serviceid><br> </client><br> <sms_info><br> <phone>13013001300</phone><br> <content>测试</content><br> </sms_info><br> </sms><br> </svc_init><br>