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 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目