dongmi1221 2017-08-14 20:24
浏览 89
已采纳

如何解析GO中忽略嵌套元素的巨大xml?

I have this XML, for example:

     <Report>
        ...
        <ElementOne Blah="bleh">
            <IgnoreElement>
                <Foo>
                   ...
                </Foo>
            </IgnoreElement>

            <WantThisElement>
                <Bar Baz="test">
                   ...
                </Bar>
                <Bar Baz="test2">
                   ...
                </Bar>
            </WantThisElement>
        </ElementOne>
        ...
    </Report>

And I'm parsing this with encode/xml:

    ... 
    decoder := xml.NewDecoder(resp.Body)
    Mystruct := MyStruct{}
    for {
    t, _ := decoder.Token()

    if t == nil {
        break
    }
    switch se := t.(type) {
    case xml.StartElement:
        if se.Name.Local == "ElementOne" {
            decoder.DecodeElement(&Mystruct, &se)
        }
    }
    ...



   type MyStruct struct{
        Blah string
        Bar []Bar
   }
   type Bar struct{
        Baz string
        ...
   }

I'm not sure if it is the best way to do it and I don't know if the decoder.DecodeElement(...) ignoring the nested elements that I don't want to parse. I want to increase perfomance with low memory cost. What the best way to parser these huge XML files?

  • 写回答

1条回答 默认 最新

  • douqi1931 2017-08-14 21:08
    关注

    Typically it is best to use XML decoder for large XML, it uses the stream and Go with selective binding (like WantThisElement>Bar) then XML decoder follows that path.

    Let's use XML content from your question to create an example.

    XML Content:

    <Report>
        <ElementOne Blah="bleh">
            <IgnoreElement>
                <Foo>
                    <FooValue>example foo value</FooValue>
                </Foo>
            </IgnoreElement>
    
            <WantThisElement>
                <Bar Baz="test">
                     <BarValue>example bar value 1</BarValue>
                </Bar>
                <Bar Baz="test2">
                    <BarValue>example bar value 2</BarValue>
                </Bar>
            </WantThisElement>
        </ElementOne>
    </Report>
    

    Structures:

    type Report struct {
        XMLName    xml.Name `xml:"Report"`
        ElementOne ElementOne
    }
    
    type ElementOne struct {
        XMLName xml.Name `xml:"ElementOne"`
        Blah    string   `xml:"Blah,attr"`
        Bar     []Bar    `xml:"WantThisElement>Bar"`
    }
    
    type Bar struct {
        XMLName  xml.Name `xml:"Bar"`
        Baz      string   `xml:"Baz,attr"`
        BarValue string   `xml:"BarValue"`
    }
    

    Play Link: https://play.golang.org/p/26xDkojeUp

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器