When you're unsure about the structure goquery is probably your best bet. Your tag names look a little odd I assume that's just for the sample. The code below uses the tag name for the key and uses the value attribute but you can modify it for any structure.
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"strings"
)
func main() {
xml := `
<?xml version="1.0"?>
<Data>
<f1 value="field value"></f1>
<f2 value="other value"></f2>
<f3 value="field value 2"></f3>
<withchildren>
<f31 value="testvalue"></f31>
<f32 value="test value 3"></f32>
</withchildren>
</Data>
`
data := make(map[string]interface{})
reader := strings.NewReader(xml)
doc, _ := goquery.NewDocumentFromReader(reader)
children := doc.Find("Data").Children()
children.Each(func(i int, s *goquery.Selection) {
val, exists := s.Attr("value")
if exists {
data[goquery.NodeName(s)] = val
}
withchildren := s.Children()
if withchildren.Length() > 0 {
withchildren.Each(func(i int, s *goquery.Selection) {
val, exists := s.Attr("value")
if exists {
data[goquery.NodeName(s)] = val
}
})
}
})
fmt.Println(data)
}
And if you really have no idea what the structure could be or how many nested elements there might be try this recursive version.
If you have multiple elements with the same name then you could modify the code to add a number with the key name. So "tag1", "tag2", etc.
package main
import (
"github.com/PuerkitoBio/goquery"
"strings"
)
func main() {
xml := `
<?xml version="1.0"?>
<Data>
<f1 value="field value"></f1>
<f2 value="other value"></f2>
<f3 value="field value 2"></f3>
<withchildren>
<f31 value="testvalue"></f31>
<f32 value="test value 3"></f32>
</withchildren>
</Data>
`
data := make(map[string]interface{})
reader := strings.NewReader(xml)
doc, _ := goquery.NewDocumentFromReader(reader)
children := doc.Find("Data").Children()
data = getElements(children)
}
func getElements(children *goquery.Selection) map[string]interface{} {
data := make(map[string]interface{})
children.Each(func(i int, s *goquery.Selection) {
val, exists := s.Attr("value")
if exists {
data[goquery.NodeName(s)] = val
}
if s.Children().Length() > 0 {
data[goquery.NodeName(s)] = getElements(s.Children())
}
})
return data
}
There are also some Go packages that I haven't used.
mxj converts xml to map[string]interface{}
goxml2json converts from xml to json.