I try to parse XML using Go, which is used to exchange data with another system. I am aware how parsing with the xml.Unmarshal function works in principle. However, in the protocol the child element of the root element <PROTO>
vary (see the examples below). For the child elements (REQUEST.DATA
, REQUEST.ACL
, REQUEST.NAC
, ... - far more than twenty different) I have defined structs with xml:...
-syntax so far - the parsing of these single child elements works.
But, is there a nice way to parse the whole message as a struct, where one varible type depends on the child element? For example with using an interface?
Example of possible XML messages:
<PROTO
version="1.00">
<REQUEST.DATA>
<DATA-L>Some information</DATA-L>
</REQUEST.DATA>
</PROTO>
<PROTO
version="1.00">
<REQUEST.ACK>
<ACK-ID>1</ACK-ID>
<REQUEST.ACK>
</PROTO>
Snippets from my Go application:
// XML: REQUEST.DATA
type DataRequest struct {
LData string `xml:"DATA-L"`
}
// XML: REQUEST.ACK
type AckRequest struct {
AckId int `xml:"ACK-ID"`
}
// XML: PROTO <============= ??
type Request struct {
Version float32 `xml:"version,attr"`
RemoteRequest {AckRequest, DataRequest, ...} ????
}
func main() {
message := `<PROTO version="1.00"><REQUEST.ACK><ACK-ID>1</ACK-ID><REQUEST.ACK></PROTO>`
data := `<REQUEST.ACK><ACK-ID>1</ACK-ID><REQUEST.ACK>`
doc := &AckRequest{}
err := xml.Unmarshal([]byte(data), &doc)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("data %+v", doc)
}
Instead of just parsing the child element data
I want to parse the whole document message
(<PROTO>
involved). And want have a structure that contains the information of the respective child element.