Using mxj
to pull XML as a map, but can't access the internal map data using ["key"]["key"]
syntax. Getting err:
.\mxjt.go:28:31: invalid operation: conf["directory"]["item_list"] (type interface {} does not support indexing)
However, the child maps do? I must be missing something here.
My golang:
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/clbanning/mxj"
)
var (
localfile string = os.Getenv("USERPROFILE") + "\\tmp-phoneconfig.cfg"
)
func main() {
f, err := ioutil.ReadFile(localfile)
if err != nil {
log.Fatal(err)
}
conf, err := mxj.NewMapXml(f)
if err != nil {
log.Fatal("err:", err.Error())
}
fmt.Println(conf["directory"]["item_list"])
}
My XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- $RCSfile: 000000000000-directory~.xml,v $ $Revision: 1.3 $ -->
<directory>
<item_list>
<item>
<ln> 1002 Shelly </ln>
<fn> Shelly </fn>
<ct> 1002 </ct>
</item>
<item>
<ln> 1003 Chris </ln>
<fn> Chris </fn>
<ct> 1003 </ct>
</item>
<item>
<ln> 1004 Extra </ln>
<fn> Extra </fn>
<ct> 1004 </ct>
</item>
<item>
<ln> 1005 Ray </ln>
<fn> Ray </fn>
<ct> 1005 </ct>
</item>
<item>
<ln> 1006 Kitchen </ln>
<fn> Kitchen </fn>
<ct> 1006 </ct>
</item>
<item>
<ln> 1007 Scott </ln>
<fn> Scott </fn>
<ct> 1007 </ct>
</item>
<item>
<ln> 1008 Heath </ln>
<fn> Heath </fn>
<ct> 1008 </ct>
</item>
<item>
<ln> 1009 Andy </ln>
<fn> Andy </fn>
<ct> 1009 </ct>
</item>
<item>
<ln> 1010 John </ln>
<fn> John </fn>
<ct> 1010 </ct>
</item>
</item_list>
</directory>
Update.
Use NewMapXmlReader(), ValuesForPath() for XML.
func main() {
file, err := os.Open(localfile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
m, err := mxj.NewMapXmlReader(file)
if err != nil {
log.Fatal(err)
}
item, err := m.ValuesForPath("directory.item_list.item")
if err != nil {
log.Fatal(err)
}
for _, v := range item {
data := v.(map[string]interface{})
fmt.Println(data["ln"], data["fn"], data["ct"])
}
}