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
-
What do you want to parse from the XML? Do you need to model the complete XML document?icza– icza2018-02-23 10:41:25 +00:00Commented Feb 23, 2018 at 10:41
-
Please include the relevant code in the body of the question.Adrian– Adrian2018-02-23 14:38:18 +00:00Commented Feb 23, 2018 at 14:38
Add a comment
|
1 Answer
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"`
}
3 Comments
Rogier Lommers
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/t6X2xbQ1PNTIain Duncan
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.
Rogier Lommers
That's it! Damn... thanks, everything works fine now.