dqvrlgi3247 2014-11-03 21:50
浏览 49
已采纳

将XML解组到地图中

As the title states I am trying to unmarshal my XML directly into a map instead of having to first unmarshal into a struct and then convert the struct into a map. I am dealing a fairly large data set and the dual conversion is taking more time than desired.

If anyone could provide any guidance on this at all it would be greatly appreciated.

XML: The classAccesses repeat and there are a few other elements.

<classAccesses>
    <apexClass>AccountRelationUtility</apexClass>
    <enabled>true</enabled>
</classAccesses>

My current struct: I parse out each of the header elements first and then create a new struct with the child elemtnts

type classAccesses struct {
    ApexClass string `xml:"apexClass"`
    Enabled   string `xml:"enabled"`
}

type diffs struct {
    ClassAccesses []classAccesses `xml:"classAccesses"`
}

Desired map: I want to keep the diffs struct, but I want the child struct "ClassAccesses" to become similar to the below map.

map[string]string {
    "ApexClass": "enabled"
}
  • 写回答

2条回答 默认 最新

  • drbxr86044 2014-11-04 11:40
    关注

    As of Go 1.3, it is not possible to unmarshal an XML document directly into a map using the standard Go library.

    The XML part of the standard library is given here, http://golang.org/pkg/encoding/xml/, there are no functions to do exactly what you ask for in the question.

    Depending on the specifics of your situation, you may have other options such as:

    • Parallelise your algorithm, i.e. read from the file and decode at the same time. This will only work well if you have multiple files to read from.
    • Write your own XML decoding algorithm in Go.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果