1

Please see this playground: https://play.golang.org/p/FOMWqhjdneg As you can see I have a XML response which I want to unmarshal into a Product struct. The XML has an "assetBlock" which contains nested nodes and extract data to Product struct. Any help would be appreciated

2
  • What do you want to parse from the XML? Do you need to model the complete XML document? Commented Feb 23, 2018 at 10:41
  • Please include the relevant code in the body of the question. Commented Feb 23, 2018 at 14:38

1 Answer 1

2

You need to make a struct for AssetBlock and all of the types below it, I've done it up to group to show you what I mean:

https://play.golang.org/p/vj_CkneHuLd

type Product struct {
    GlobalID   string     `xml:"globalId"`
    Title      string     `xml:"title"`
    ChunkID    int        `xml:"gpcChunkId"`
    AssetBlock assetBlock `xml:"assetBlock"`
}
type assetBlock struct {
    Images images `xml:"images"`
}
type images struct {
    GroupList groupList `xml:"groupList"`
}
type groupList struct {
    Groups []group `xml:"group"`
}
type group struct {
    Usage string `xml:"usage"`
    Size string `xml:"size"`
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Really helpful. However, I tried to enter the asset-part, but for some reason I can't figure out how to get the urls. See this playfround: play.golang.org/p/t6X2xbQ1PNT
You need to capitalize the property names: play.golang.org/p/AUnvdlkgq2E any lower case first letter is private in go and will be ignore by the XML encoders.
That's it! Damn... thanks, everything works fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.