代碼: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 結構之一,而不是完整數(shù)組。我該如何解決?這是一個問題減少的去游樂場:http : //play.golang.org/p/5uRJ6Eu8jK
2 回答

茅侃侃
TA貢獻1842條經(jīng)驗 獲得超21個贊
XML 解析器需要具有單個頂級元素的格式良好的 XML 文檔。它正在讀取第一個元素,假設這是整個文檔,然后停在那里。
從元素的父元素開始,HostSystemIdentificationInfo然后對其進行解組:
<whatever>
<HostSystemIdentificationInfo .../>
<HostSystemIdentificationInfo .../>
<HostSystemIdentificationInfo .../>
</whatever>
type HostSystemIdentificationInfo struct {
IdentifierValue string
// ...
}
type whatever struct {
Info []HostSystemIdentificationInfo `xml:"HostSystemIdentificationInfo"`
}
(如有必要,將 XML 包裝在虛假的頂級元素中)。
- 2 回答
- 0 關注
- 451 瀏覽
添加回答
舉報
0/150
提交
取消