I am having some problem trying to unmarshall XMLRPC into Go Struct
The data I want to unmarshall is structured like this:
rawXMLData := `<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>RESPONSECODE</name>
<value><string>68</string></value>
</member>
<member>
<name>REQUESTID</name>
<value><string>120001</string></value>
</member>
<member>
<name>MESSAGE</name>
<value><string>Currently Unavailable, Please try again later </string></value>
</member>
<member>
<name>TRANSACTIONID</name>
<value><string>271231425</string></value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
`
and I'm trying to put the data into this struct
type Response struct {
RESPONSECODE string `xml:RESPONSECODE`
REQUESTID string `xml:REQUESTID`
MESSAGE string `xml:MESSAGE`
TRANSACTIONID string `xml:TRANSACTIONID`
}
But it doesn't work, when I tried to unmarshall it with
val := Response{}
err := xml.Unmarshal([]byte(rawXMLData), &val)
if err != nil {
fmt.Println(err)
}
fmt.Println("val = ", val)
//in terminal
//val = { }
The value that need to be taken were <name> and <value>, that represent the label and its value (in corresponding golang struct).
I think the problem was because the field label (for the golang struct) is inside the xmltag value, and not the xmltag itself. And I haven't found any clue on how to access this value.
Currently I tried a new struct like this, then tried to manually parse the array into the struct.
type Member struct {
Name string `xml:"name"`
Value string `xml:"value>string"`
}
type Resps struct {
XMLName xml.Name `xml:"methodResponse"`
Members []Member `xml:"params>param>value>struct>member"`
}
and manage to get this
value := Resps{}
xml.Unmarshal([]byte(rawXMLData), &value)
fmt.Println(value.Members)
// terminal result:
// [{RESPONSECODE 68} {REQUESTID 120001} {MESSAGE Currently Unavailable, Please try again later} //{TRANSACTIONID 271231425}]
//
// then put each of the value.Members into corresponding struct field
Are there any other solution to directly take the rawXML data into the first struct? or do we need to parse it in two step like I did?