gpt4 book ai didi

xml - 如何使用 Golang 获取 XML 中深层元素的元素名称

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:23 25 4
gpt4 key购买 nike

我得到了以下 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`
}

是否可以在 Instruction 字段中写入 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 时包含 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)
}
}
}
}

关于xml - 如何使用 Golang 获取 XML 中深层元素的元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com