请教一个 XML 序列化问题

2022-06-02 23:37:21 +08:00
 seers

我要生成如下 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 结构还在 请问为什么会这样,然后如何修复呢,感谢。

1458 次点击
所在节点    Go 编程语言
4 条回复
CEBBCAT
2022-06-03 01:02:01 +08:00
没用过 Go 操作 xml ,但我看你的 Client 、Sms_info 是内嵌的,考虑一下使用 https://www.onlinetool.io/xmltogo/ 生成的结构?

另外打印的时候可以用 fmt.Printf("%+v", v),这样有字段名,好理解一点
leonard916
2022-06-03 10:13:33 +08:00
结构里 代码写写全,IDE 没报错?
seers
2022-06-03 11:43:56 +08:00
@CEBBCAT 感谢,通过页面生成的结构成功了
MeetTheFuture
2022-06-04 10:00:20 +08:00
```go
package main

import (
"encoding/xml"
"os"
)

var data = `<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>`

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 {
Ver string `xml:"ver,attr"`
Client
Sms_info
}

type Svc_init struct {
XMLName xml.Name `xml:"svc_init"`
Ver string `xml:"ver,attr"`
Sms Sms `xml:"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}

output, _ := xml.MarshalIndent(v, " ", " ")

os.Stdout.Write(output)
}
```
给 Sms 添加了一个 `xml:"sms"` 就好了
```xml
<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>
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/857044

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX