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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据