How to parser huge xml in GO ignoring nested elements? -
i have xml, 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 encode/xml:
... decoder := xml.newdecoder(resp.body) mystruct := mystruct{} { 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 best way , don't know if decoder.decodeelement(...) ignoring nested elements don't want parse. want increase perfomance low memory cost. best way parser these huge xml files?
typically best use xml decoder large xml, uses stream , go selective binding (like wantthiselement>bar
) xml decoder follows path.
let's use xml content question create 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
Comments
Post a Comment