To handle a xml API response result, need to catch multiple cases. For a success case and a failure case, I can define 2 struct to handle both of them. The source as: go playground.
If the response data has many results, and they don't have the same xml structure, we can define all the struct types in go. But is there an easy way to catch its data?
I found go's xml UnmarshalXML method can rewrite xml data. Package xml
But use it need to set to special and unique struct first:
func (r *PostSuccessResponse) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return nil
}
Is it possible to use startElement.Name.Local to iterate over all the response xml data to check what element it has, then use which go struct to handle? Such as if found Fault then use PostFailureResponse struct, if found return then use PostSuccessResponse struct.
Based on the suggestion from comments I recreated your code as follows:
Output: