I looked for some examples in the official documentation of golang
and in some online tutorials but only find simple examples see:
<data>
<person>
<firstname>Nic</firstname>
<lastname>Raboy</lastname>
<address>
<city>San Francisco</city>
<state>CA</state>
</address>
</person>
<person>
<firstname>Maria</firstname>
<lastname>Raboy</lastname>
</person>
</data>
To read this structure xml
I only need 2 struct
being: data
and person
, this example was taken from this site along with the resolution: thepolyglotdeveloper
Structs:
type Data struct {
XMLName xml.Name 'xml:"data" json:"-"'
PersonList []Person 'xml:"person" json:"people"'
}
type Person struct {
XMLName xml.Name 'xml:"person" json:"-"'
Firstname string 'xml:"firstname" json:"firstname"'
Lastname string 'xml:"lastname" json:"lastname"'
Address *Address 'xml:"address" json:"address,omitempty"'
}
type Address struct {
City string 'xml:"city" json:"city,omitempty"'
State string 'xml:"state" json:"state,omitempty"'
}
My question is how do I do when a xml
has more than one node as in the example below:
<SuccessResponse>
<Head>
<RequestId/>
<RequestAction>XPTP</RequestAction>
<ResponseType>XPTU</ResponseType>
<Timestamp>TIMESTAMP</Timestamp>
</Head>
<Body>
<Products>
<Product>...</Product>
<Product>...</Product>
</Products>
</Body>
</SuccessResponse>
I'm going to need to create a struct
to SuccessResponse
and within it I must have outa struct
with the name Body
and within Body
Products
and so on?