The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
seers

请教一个 XML 序列化问题

  •  
  •   seers · Jun 2, 2022 · 1878 views
    This topic created in 1446 days ago, the information mentioned may be changed or developed.

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

    4 replies    2022-06-04 10:00:20 +08:00
    CEBBCAT
        1
    CEBBCAT  
       Jun 3, 2022   ❤️ 1
    没用过 Go 操作 xml ,但我看你的 Client 、Sms_info 是内嵌的,考虑一下使用 https://www.onlinetool.io/xmltogo/ 生成的结构?

    另外打印的时候可以用 fmt.Printf("%+v", v),这样有字段名,好理解一点
    leonard916
        2
    leonard916  
       Jun 3, 2022
    结构里 代码写写全,IDE 没报错?
    seers
        3
    seers  
    OP
       Jun 3, 2022
    @CEBBCAT 感谢,通过页面生成的结构成功了
    MeetTheFuture
        4
    MeetTheFuture  
       Jun 4, 2022   ❤️ 1
    ```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>
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2979 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 14:30 · PVG 22:30 · LAX 07:30 · JFK 10:30
    ♥ Do have faith in what you're doing.