本文介绍了将XML解组成映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我试图将我的XML直接解组成映射,而不是先解组成一个结构,然后将结构转换为映射。我正在处理一个相当大的数据集,双重转换需要的时间比预期的要多。



如果任何人都可以提供任何指导,我们将不胜感激。

XML:classAccesses重复使用,还有其他一些元素。

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

我当前的结构:我先解析出每个头元素,然后用子元素

 类型classAccesses结构{
ApexClass字符串`xml:apexClass`
启用的字符串` xml:enabled`
}

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

期望的地图:我想保留差异结构,但我希望子结构ClassAccesses变得类似于下面的地图。

  map [string] string {
ApexClass:enabled
}


解决方案
从Go 1.3开始,直接解组一个XML文档是不可能的使用标准的Go库进行映射。



标准库的XML部分在这里给出,,th在这个问题中,你不需要按照你的要求去做。



根据你的具体情况,你可能有其他选择,例如:




  • 并行化您的算法,即从文件读取并同时解码。如果您有多个要读取的文件,这样做只会有效。

  • 在Go中编写您自己的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"
}
解决方案

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.

这篇关于将XML解组成映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:58