download2014711 2018-07-26 13:04
浏览 136

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

I got the following XML structure

XML-structure:

<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>

and created the following struct in 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`
}

Is it possible to write in the Instruction field the element name from the elements in the bar element (ab, cd, de)? I get the element name with xml.Name but i got this only inside the ab, cd, ef struct.

The other question: is it possible tha inside the Bar struct only the struct from the XML is shown?

At the moment it looks like:

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

It should be:

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

I receive the XMl as a string and unmarshal it with the following code:

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
}
  • 写回答

1条回答 默认 最新

  • doucuo8618 2018-07-26 13:51
    关注

    The following snipet provide complete decoding XML struct, If you do not want include ab, cd, and de type while encoding to XML just add ommitempty to xml tag.

    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
    ", step.Command.Bar.Ab)
                }
                if step.Command.Bar.Cd != nil {
                    fmt.Printf("cd: %v
    ", step.Command.Bar.Cd)
                }
                if step.Command.Bar.De != nil {
                    fmt.Printf("de: %v
    ", step.Command.Bar.De)
                }
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭