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 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私