Suppose I have the following xml:
<main symbol="X">
<blockA main_score="3">
<a score="0"/>
</blockA>
<blockB>
<b id="3" name="Mike"/>
</blockB>
</main>
And I'd like to define the following struct (blank tags are parts I want to solve):
type Result struct {
XMLName xml.Name `xml:"main"`
Symbol string `xml:"symbol,attr"`
MainScore int
Score int
Id int
Name string
}
And what I'd like to get is the following struct:
symbol: X
main_score: 3
score: 0
id: 3
name: Mike
So how can I define XML tags that goes into inner elements (blockA
, blockB
) and also reaches its attribute values (main_score
) and inner elements (score
, id
, name
)?
I can address the issue here by defining yet another structs and embedding them within the parent Result
struct. However, is it still feasible to NOT use the embedding struct and just define struct tags within the main struct and have it process the whole elements?
Thanks.
I don't think it is currently possible to unmarshal that XML into your structure with the current version of the package.
If it was supported, you'd want to annotate MainScore
with:
MainScore int `xml:"blockA>main_score,attr"`
i.e. pick the main_score
attribute from the blockA
sub-element. This does not currently work though, as described in issue 3688.
At the moment, I think you will need to create nested structs to fully unmarshal the data you're after.