首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过mxj访问XML数据

通过mxj访问XML数据
EN

Stack Overflow用户
提问于 2019-03-30 00:12:05
回答 1查看 56关注 0票数 0

使用mxj将XML作为地图提取,但不能使用["key"]["key"]语法访问内部地图数据。获取错误:

代码语言:javascript
复制
.\mxjt.go:28:31: invalid operation: conf["directory"]["item_list"] (type interface {} does not support indexing)

但是,子地图呢?我一定漏掉了什么。

我的戈朗:

代码语言:javascript
复制
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"])
}

我的XML:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2019-03-30 00:47:01

更新。

对XML使用NewMapXmlReader(),ValuesForPath()。

代码语言:javascript
复制
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"])
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55421485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档