xml - 尝试使用 Golang encoding/xml 将结构编码为 xml 具有交替重复模式的结构

标签 xml go struct xml-encoding

我正在尝试使用 encoding/xml 包创建一个具有以下顺序的 xml 文件,并且我已经为静态子元素和转换子元素定义了结构,现在的问题是我需要以重复格式使用它,所以我创建了另一个结构来保存静态和过渡结构的 slice ,但结果是静态元素都出现在过渡元素之前,但我需要它们按顺序交替。
这是我想要的结构:

<background>
    //...
    <static>
        <duration></duration>
        <file></file>
    </static>

    <transition>
        <duration>
        <from></from>
        <to></to>
    </transition>

    <static>
        <duration></duration>
        <file></file>
    </static>

    <transition>
        <duration>
        <from></from>
        <to></to>
    </transition>
    //...
</background>

但这就是我得到的:
<background>
    //...
    <static>
        <duration></duration>
        <file></file>
    </static>
    <static>
        <duration></duration>
        <file></file>
    </static>

    <transition>
        <duration>
        <from></from>
        <to></to>
    </transition>
    <transition>
        <duration>
        <from></from>
        <to></to>
    </transition>
    //...
</background>

关于我如何做到这一点的任何帮助。
这些是我创建的结构:
type Static struct {
Duration int    `xml:"duration"`
File     string `xml:"file"`
}
type Transition struct {
    Duration float64 `xml:"duration"`
    From     string  `xml:"from"`
    To       string  `xml:"to"`
}
type ST struct {
    Static     []Static     `xml:"static"`
    Transition []Transition `xml:"transition"`
}

type Background struct {
    XMLName   xml.Name  `xml:"background"`
    Comment   string    `xml:",comment"`
    ST
}

最佳答案



type Static struct {
    XMLName  string `xml:"static"`
    Duration int    `xml:"duration"`
    File     string `xml:"file"`
}

type Transition struct {
    XMLName  string  `xml:"transition"`
    Duration float64 `xml:"duration"`
    From     string  `xml:"from"`
    To       string  `xml:"to"`
}

type Background struct {
    XMLName string `xml:"background"`
    Items   []interface{}
}

bk := Background{
    Items: []interface{}{
        &Static{
            Duration: 11,
            File:     "foo",
        },
        &Transition{
            Duration: 22,
            From:     "aaa",
            To:       "bbb",
        },
        &Static{
            Duration: 33,
            File:     "bar",
        },
        &Transition{
            Duration: 44,
            From:     "xxx",
            To:       "yyy",
        },
    },
}

out, err := xml.Marshal(&bk)

应该覆盖您的 (playground)。

请注意,为了获得一个正确序列化的——在以指定顺序依次跟随的意义上——元素列表,您必须使用反射(reflect)这一点的数据结构;
在这种简单的情况下,Go slice 最适合。

在复杂的情况下,可以使您的自定义类型实现 encoding/xml.Marshaler接口(interface)并使用较低级别的encoding/xml以您希望的任何顺序对单个 emenets 进行编码的设施。

关于xml - 尝试使用 Golang encoding/xml 将结构编码为 xml 具有交替重复模式的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61603334/

相关文章:

Java XMLInputFactory - 使用 .getData() 读取数据时截断文本

python - 如何从 go 语言的 main 中获取不同的退出代码,如 2 或 3?

c - 需要帮助将数组结构更改为结构数组

c - strcpy 产生重复的存储项

java - 如何在滑动菜单中将标题添加到 ListView ?

php - 使用PHP从XML youtube视频获取视频ID

c# - powerpoint 记录排练时间

mongodb - 如何在 golang 和 mongodb 中通过 id 查找

go - 在 Go 中将 Gonum 用于图形算法

c - 'typedef in C' 有什么用?