代码:type HostSystemIdentificationInfo []struct { IdentiferValue string `xml:"identifierValue"` IdentiferType struct { Label string `xml:"label"` Summary string `xml:"summary"` Key string `xml:"key"` } `xml:"identifierType"`}func vsphereHost(v *vsphere.Vsphere, md *opentsdb.MultiDataPoint) error { res, err := v.Info("HostSystem", []string{ "name", "summary.hardware.cpuMhz", "summary.hardware.memorySize", // bytes "summary.hardware.numCpuCores", "summary.hardware.numCpuCores", "summary.quickStats.overallCpuUsage", // MHz "summary.quickStats.overallMemoryUsage", // MB "summary.hardware.otherIdentifyingInfo", "summary.hardware.model", }) for _, r := range res { for _, p := range r.Props { if p.Name == "summary.hardware.otherIdentifyingInfo" { var t HostSystemIdentificationInfo fmt.Println(p.Val.Inner) err := xml.Unmarshal([]byte(p.Val.Inner), &t) if err != nil { return err } fmt.Println(t) } } }所以问题是当我解组时,我只在结果中得到 HostSystemIdentification 结构之一,而不是完整数组。我该如何解决?这是一个问题减少的去游乐场:http : //play.golang.org/p/5uRJ6Eu8jK
2 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
XML 解析器需要具有单个顶级元素的格式良好的 XML 文档。它正在读取第一个元素,假设这是整个文档,然后停在那里。
从元素的父元素开始,HostSystemIdentificationInfo然后对其进行解组:
<whatever>
<HostSystemIdentificationInfo .../>
<HostSystemIdentificationInfo .../>
<HostSystemIdentificationInfo .../>
</whatever>
type HostSystemIdentificationInfo struct {
IdentifierValue string
// ...
}
type whatever struct {
Info []HostSystemIdentificationInfo `xml:"HostSystemIdentificationInfo"`
}
(如有必要,将 XML 包装在虚假的顶级元素中)。
- 2 回答
- 0 关注
- 421 浏览
添加回答
举报
0/150
提交
取消