1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
// https://groups.google.com/forum/#!topic/golang-nuts/-N9Toa6qlu8
// shows that you can extract attribute values directly from tag/key path.
// NOTE: attribute values are encoded by prepending a hyphen, '-'.
package main
import (
"fmt"
"github.com/clbanning/mxj/v2"
)
var xmldata = []byte(`
<doc>
<some_tag>
<geoInfo>
<city name="SEATTLE"/>
<state name="WA"/>
<country name="USA"/>
</geoInfo>
<geoInfo>
<city name="VANCOUVER"/>
<state name="BC"/>
<country name="CA"/>
</geoInfo>
<geoInfo>
<city name="LONDON"/>
<country name="UK"/>
</geoInfo>
</some_tag>
</doc>`)
func main() {
fmt.Println("xmldata:", string(xmldata))
m, merr := mxj.NewMapXml(xmldata)
if merr != nil {
fmt.Println("merr:", merr)
return
}
// Attributes are keys with prepended hyphen, '-'.
values, err := m.ValuesForPath("doc.some_tag.geoInfo.country.-name")
if err != nil {
fmt.Println("err:", err.Error())
}
for _, v := range values {
fmt.Println("v:", v)
}
}
|