Convert multiple nodes to XML for struct

Asked

Viewed 133 times

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 them: 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 doubt is how I’m gonna 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 gonna need to create a struct for SuccessResponse and inside it I need to have struct with the name Body and within Body Products and so on?

1 answer

1


Yes, you should create the nested structures in the same way as the original document. In the example you gave, it 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 inner structure and pull them further out. 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 country.

It is possible to construct 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"`
}

Check out Go Playground

  • what is the XMLName xml.Name \xml:"person" json:"-"`na primeira linha dePerson`?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.