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 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏