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 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数