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

如何解析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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?