I have to parse many XML documents, I already know the logic to parse the files, but the issue is, the tag for each xml file is different.
This is what I have so far
type Data struct {
Rows []Info `xml:"domain-google.com"` // <- This is custom and needs to change (e.g. the next xml doc tag will be `xml:"domain-facebook.com"`
}
type Info struct {
Domain string `xml:"domain"`
}
// example logic for parsing xml
xmlFile, err := http.Get("somefiles.xml")
if err != nil { fmt.Println(err) }
var data Data
if err = xml.NewDecoder(xmlFile.Body).Decode(&data); err != nil {
fmt.Println(err)
}
for _, rows := range data.Rows {
fmt.Println(rows.Domain)
}
example xml documents
<response>
<!-- This tag is what is custom every time -->
<domain-google.com>
<domain>google.com</domain>
</domain-google.com>
</response>
<response>
<!-- This tag is what is custom every time -->
<domain-facebook.com>
<domain>facebook.com</domain>
</domain-facebook.com>
</response>
Any idea how to tackle this issue?
Thank you in advanced.
As XML goes, this is pretty bad; tag names should not be used for data, only tag bodies and attribute values should hold data. But, all is not lost! Per the documentation on encoding/xml
, you can use the ,any
tag to map a field that should contain "a sub-element that hasn't matched any of the above rules". You can use this as a sort of wildcard, to match any child element not otherwise mapped - in your case, a child element whose tag name you don't know at compile time:
type Data struct {
Rows []Info `xml:",any"`
}
Playground example: https://play.golang.org/p/lo6bJBRvgV