Currently, I'm implementing custom unmarshal xml for Bar
which will unmarshal base64 string to Bar
struct.
But when I run this program, it's stackoverflow like infinite recursive unmarshal my custom unmarshal.
GO Playground: https://play.golang.org/p/QD4IdxhZr1Q
package main
import (
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
)
type Foo struct {
A Bar `xml:"a"`
}
type Bar struct {
B string `xml:"b"`
C string `xml:"c"`
D string `xml:"d"`
}
func main() {
var foo Foo
// Input
s := `<Foo><a>PGI+Yi10ZXN0PC9iPjxjPmMtdGVzdDwvYz48ZD5kLXRlc3Q8L2Q+Cg==</a></Foo>`
err := xml.Unmarshal([]byte(s), &foo)
if err != nil {
panic(err)
}
fmt.Println("Foo:", foo)
}
func (m *Bar) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
err := d.DecodeElement(&s, &start)
fmt.Println("Decode Element:", s)
if err != nil {
return err
}
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
fmt.Println("Base64 Decoded string:", string(data))
var buf bytes.Buffer
buf.WriteString("<Bar>")
buf.WriteString(string(data))
buf.WriteString("</Bar>")
result := buf.String()
fmt.Println("After add root node:", result)
xml.Unmarshal([]byte(result), &m)
return nil
}
Thank you!