Convert multiple nodes in XML to struct

1

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?

    
asked by anonymous 18.01.2018 / 01:31

1 answer

1

Yes, you should create the nested structures in the same way as the original document. In the example you gave, this already happens on a single level with Person and Address .

What can make it easier for you is to access the specific fields you want in the innermost structure and pull them out further. Following the example you took, imagine that city is a structure composed of the name of the city, state and country:

<data>
    <person>
        <firstname>Nic</firstname>
        <lastname>Raboy</lastname>
        <address>
            <city>
                <name>San Francisco</name>
                <country>USA</country>
                <state>CA</state>
            </city>
        </address>
    </person>
    <person>
        <firstname>Maria</firstname>
        <lastname>Raboy</lastname>
    </person>
</data>

And suppose you're only interested in the name of the city and the name of the country.

You can build the following structure:

Person struct {
    XMLName   xml.Name 'xml:"person" json:"-"'
    Firstname string   'xml:"firstname" json:"firstname"'
    Lastname  string   'xml:"lastname" json:"lastname"'
    City      string   'xml:"address>city>name" json:"city,omitempty"'
    Country   string   'xml:"address>city>country" json:"country,omitempty"'
}

See on Go Playground

    
18.01.2018 / 17:54