【问题标题】:How to get the element name of a deeper lying element in XML with Golang如何使用 Golang 在 XML 中获取更深层次元素的元素名称
【发布时间】:2018-07-26 13:04:27
【问题描述】:

我得到了以下 XML 结构

XML 结构:

<assay>
    <step id="1">
        <command>
            <bar>
                <ab>structure 1</ab>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="2">
        <command>
            <bar>
                <cd>structure 2</cd>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="3">
        <command>
            <bar>
                <de>structure 3</de>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="4">
        <command>
            <bar>
                <ab>structure 1</ab>
            </bar>
            <duration>5</duration>
        </command>
    </step>
</assay>

并在 golang 中创建了以下结构

type Assay struct {
    Steps []struct {
        ID           int     `xml:"id"`
        Duration     int     `xml:"duration"`
        Instruction  string  `xml:"command>bar"`
        Command      Command `xml:"command"`
    } `xml:"step"`
}

type Command struct {
    Bar    struct {
        Ab *Ab struct {}  `xml:"ab"`
        Cd *Cd struct {}  `xml:"cd"`
        De *De struct {}  `xml:"de"`
    } `xml:bar`
}

是否可以在指令字段中写入 bar 元素(ab、cd、de)中元素的元素名称?我用 xml.Name 得到元素名称,但我只在 ab、cd、ef 结构中得到这个。

另一个问题:是否有可能在 Bar 结构中只显示来自 XML 的结构?

目前看起来是这样的:

Bar {
    Ab: {...some Informations}
    Cd: {nil}
    De:{nil}
}
Bar {
    Ab: {nil}
    Cd: {...some Informations}
    De:{nil}
}

应该是:

Bar {
    Ab: {...some Informations}
}
Bar {
    Cd: {...some Informations}
}

我将 XMl 作为字符串接收并使用以下代码对其进行解组:

func ExtractAssay(stringXML string) (structXML requests.Assay) {
    stringByte := []byte(stringXML)
    err := xml.Unmarshal(stringByte, &structXML)
    if nil != err {
        fmt.Println("Error unmarshalling from XML", err)
        return
    }
    return structXML
}

【问题讨论】:

  • 请发布您尝试将 xml 解组为给定结构的代码

标签: xml go


【解决方案1】:

以下片段提供完整的解码 XML 结构, 如果您不想在编码为 XML 时包含 ab、cd 和 de 类型,只需将 ommitempty 添加到 xml 标记。

var XML = `<assay>
    <step id="1">
        <command>
            <bar>
                <ab>structure 1</ab>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="2">
        <command>
            <bar>
                <cd>structure 2</cd>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="3">
        <command>
            <bar>
                <de>structure 3</de>
            </bar>
            <duration>5</duration>
        </command>
    </step>
    <step id="4">
        <command>
            <bar>
                <ab>structure 1</ab>
            </bar>
            <duration>5</duration>
        </command>
    </step>
</assay>`



type Assay struct {
    Steps []struct {
        ID           int     `xml:"id"`
        Duration     int     `xml:"duration"`
        Command      Command `xml:"command"`
    } `xml:"step"`
}

type Ab struct {
    Value string `xml:",chardata"`
}
type Cd struct {
    Value string `xml:",chardata"`
}
type De struct {
    Value string `xml:",chardata"`
}
type Bar struct {
    Ab *Ab  `xml:"ab,omitempty"`
    Cd *Cd  `xml:"cd,omitempty"`
    De *De `xml:"de,omitempty"`
}

type Command struct {
    Bar  *Bar `xml:"bar"`
}




func main() {

    var asSay = &Assay{}
    err := xml.Unmarshal([]byte(XML), &asSay)
    if err != nil {
        log.Fatal(err)
    }
    for _, step := range asSay.Steps {
        if step.Command.Bar != nil {
            if step.Command.Bar.Ab != nil {
                fmt.Printf("ab: %v\n", step.Command.Bar.Ab)
            }
            if step.Command.Bar.Cd != nil {
                fmt.Printf("cd: %v\n", step.Command.Bar.Cd)
            }
            if step.Command.Bar.De != nil {
                fmt.Printf("de: %v\n", step.Command.Bar.De)
            }
        }
    }
}

【讨论】:

  • 感谢您的快速答复。我认为“omitempty”标签仅用于创建 XML。通过您的示例,我还得到了我在问题中描述的带有 nil 元素的结构。
猜你喜欢
  • 2022-01-04
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 2015-08-31
  • 2014-03-01
  • 1970-01-01
相关资源
最近更新 更多