I want to Unmarshal the innerXml and its attributes. I write a Unmarshal function to implement this, but it looks like the function is in an infinite loop. The error information is
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
The example is here.
I don't know why this happens. Could someone help me, thanks.
Update1: Thanks Ainar-G. I tried his example. It works as get the innerXml as chardata which I did't find. If I change the example as this, the result is empty, it should include all the raw xml in .
Update2: I find a solution, but may be a little wordy. code.
In your UnmarshalXML
method you call xml.(*Decoder).DecodeElement
, which in turn calls UnmarshalXML
, etc. This creates the infinite loop. Either create a wrapper struct, or unmarshal only a part of your struct in your UnmarshalXML
.
EDIT: if you want to unmarshal all attributes of a node, see the example in this answer.
Working example:
func (in *innerXml) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
in.XMLName = start.Name
in.Attrs = make(map[string]string)
for _, attr := range start.Attr {
in.Attrs[attr.Name.Local] = attr.Value
}
err := d.DecodeElement(&in.Value, &start)
if err != nil {
return err
}
return nil
}
Playground: http://play.golang.org/p/TLcqFSyn94