drxt70655 2015-05-17 10:14
浏览 54
已采纳

xml Unmarshal innerXml及其属性

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.

  • 写回答

1条回答 默认 最新

  • douqixia7942 2015-05-17 10:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?