
当xml字符串包含多个同级根元素(如多个 `
在Go中,标准库的 encoding/xml.Unmarshal 函数要求输入XML必须有唯一根节点(well-formed XML document)。而问题中的XML片段实际是多个同级
正确的解决方案是使用 xml.Decoder,它支持流式解析,可对同一输入缓冲区连续调用 Decode,每次读取并解析一个独立的XML元素:
d := xml.NewDecoder(bytes.NewBufferString(xmlFragment))
var results []HostSystemIdentificationInfo
for {
var item struct {
IdentiferValue string `xml:"identifierValue"`
IdentiferType struct {
Label string `xml:"label"`
Summary string `xml:"summary"`
Key string `xml:"key"`
} `xml:"identifierType"`
}
err := d.Decode(&item)
if err == io.EOF {
break // 所有元素已读完
}
if err != nil {
log.Printf("XML decode error: %v", err)
continue // 跳过错误项,避免中断整个流程
}
results = append(results, HostSystemIdentificationInfo{item})
}⚠️ 注意事项:不要复用切片变量地址:如原代码中 var t HostSystemIdentificationInfo 并传入 &t,因 HostSystemIdentificationInfo 是切片类型别名,Decode(&t) 实际尝试将整个XML结构解码为一个切片元素(而非追加),行为不可控。推荐改为解码为单个匿名结构体,再手动 append 到目标切片。命名一致性:结构体字段标签中 IdentiferValue / IdentiferType 拼写应为 IdentifierValue / IdentifierType(原文本含笔误),否则可能因大小写或拼写不匹配导致字段为空。xsi:type 属性处理:示例XML含 xsi:type 属性,若需保留或校验类型信息,可在结构体中添加对应字段:XSIType stringxml:"xsi:type,attr``。健壮性增强:生产环境建议添加 strings.TrimSpace 预处理XML片段,并用 xml.CharData 处理文本节点空白,避免 identifierValue 前导空格干扰。
综上,核心原则是:非标准XML片段 → 用 xml.Decoder 流式解析;标准单根XML → 用 xml.Unmarshal。合理选择解析方式,才能可靠处理vSphere等系统返回的类XML片段数据。










