dousuo2812 2015-12-11 10:32
浏览 37
已采纳

如何在Go中使用可选标签封送XML

The code for my problem is here: https://play.golang.org/p/X8Ey2hqmxL

package main

import (
    "encoding/xml"
    "fmt"
    "log"
)

type Carriage struct {
    MainCarriage interface{} `xml:"mainCarriage"`
}

type SeaCarriage struct {
    Sea          xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 sea"`
    LoadFactor   float64  `xml:"loadFactor,attr"`
    SeaCargoType string   `xml:"seaCargoType,attr"`
}

type RoadCarriage struct {
    Road xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 road"`
}

func main() {

    fr := Carriage{
        MainCarriage: SeaCarriage{
            LoadFactor:   70,
            SeaCargoType: "Container",
        },
    }
    xmlBlob, err := xml.MarshalIndent(&fr, "", "\t")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(xmlBlob))
}

I need to marshall data into SOAP xml. I'm currently getting this result:

<Carriage>
    <mainCarriage loadFactor="70" seaCargoType="Container">
        <sea xmlns="http://www.example.com/XMLSchema/standard/2012"></sea>
    </mainCarriage>
</Carriage>

But I need this result:

<Carriage>
    <mainCarriage>
        <sea xmlns="http://www.example.com/XMLSchema/standard/2012" loadFactor="70" seaCargoType="Container"></sea>
    </mainCarriage>
</Carriage>

No matter what I try I cannot marshal the structs so that the loadFactor and the seaCargoType are attrs of the sea tag.

The Carriage struct takes an empty interface because depending on the shipment type the tag should be either sea or road but never both.

  • 写回答

1条回答 默认 最新

  • dongnanman9093 2015-12-11 10:46
    关注

    Put >. after mainCarriage field tag to indicate that you want to put contents of the field inside mainCarriage tag. Change the name of Sea field to XMLName as required by marshaller.

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "log"
    )
    
    type Carriage struct {
        MainCarriage interface{} `xml:"mainCarriage>."`
    }
    
    type SeaCarriage struct {
        XMLName      xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 sea"`
        LoadFactor   float64  `xml:"loadFactor,attr"`
        SeaCargoType string   `xml:"seaCargoType,attr"`
    }
    
    type RoadCarriage struct {
        Road xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 road"`
    }
    
    func main() {
    
        fr := Carriage{
            MainCarriage: SeaCarriage{
                LoadFactor:   70,
                SeaCargoType: "Container",
            },
        }
        xmlBlob, err := xml.MarshalIndent(&fr, "", "\t")
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(string(xmlBlob))
    }
    

    <kbd>Playground</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化