How to unmarshal data from the Item block from XML? Rates and Item blocks have fields with the same names. I get XML data from API and save it to DB, I use gorm for DB.
My code saves data (name and title) from Rates block. I need to save data (name and title) from Item block.
i see u used Root type for root variable, i used Item type for such variable. In your case i receive error when writing to db: “invalid field found for struct Rates’s field XMLName: define a valid foreign key for relations or implement the Valuer/Scanner interface”
ChatGPT helped me to solve my problem with adding new structs and additional code:
type Rate struct {
Name string `xml:"name"`
Title string `xml:"title"`
Items []Item `xml:"item"`
}
type Item struct {
Name string `xml:"name"`
Title string `xml:"title"`
}
type RateModel struct {
gorm.Model
Name string
Title string
Item []ItemModel
}
type ItemModel struct {
gorm.Model
RateModelID uint
Name string
Title string
}
// Create a new RateModel instance
rateModel := RateModel{
Name: rate.Name,
Title: rate.Title,
}
// Convert and save items
for _, item := range rate.Items {
rateModel.Item = append(rateModel.Item, ItemModel{
Name: item.Name,
Title: item.Title,
})
}