dongruo0909 2016-04-14 13:56
浏览 119
已采纳

如何在Go中使用各种元素解析巨大的XML文件?

How can you parse a huge XML file that's having various elements (i.e. not same element repeated multiple times).

Example:

<stuff>
    <header>...</header>
    <item>...</item>
    ...
    <item>...</item>
    <something>...</sometihng>
</stuff>

I want to write a script in Go that would allow me to split this file in multiple smaller files with specific amount of tags per file. All examples on how to parse XML with Go seems to rely on knowing the elements that you have in the file.

Can the file be parsed without knowing that? Something like for each element in XML no matter what element is there (header, item, something, etc...)

  • 写回答

4条回答 默认 最新

  • dongzanghui4624 2016-04-14 15:45
    关注

    Use the standard xml Decoder.

    Call Token to read tokens one by one. When a start element of interest is found, call DecodeElement to decode the element to a Go value.

    Here's a sketch of how to use the decoder:

    d := xml.NewDecoder(r)
    for {
        t, tokenErr := d.Token()
        if tokenErr != nil {
            if tokenErr == io.EOF {
               break
            }
            // handle error
        }
        switch t := t.(type) {
        case xml.StartElement:
            if t.Name.Space == "foo" && t.Name.Local == "bar" {
                var b bar
                if err := d.DecodeElement(&b, &t); err != nil {
                    // handle error
                }
                // do something with b
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求