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