
go 的 `xml.unmarshal` 仅能解析单个顶层 xml 元素;当输入是多个同名、并列的根级元素(如多个 `
在 Go 中处理 XML 数据时,一个常见误区是假设 xml.Unmarshal 能自动识别并解析多个同级、无父容器的 XML 元素(例如 vSphere 返回的 otherIdentifyingInfo 字段中连续出现的多个
✅ 正确解法是改用 xml.Decoder,它支持流式解析,可反复调用 Decode 方法逐个读取独立的 XML 元素:
func parseHostIdentifiers(xmlData string) ([]struct {
IdentifierValue string `xml:"identifierValue"`
IdentifierType struct {
Label string `xml:"label"`
Summary string `xml:"summary"`
Key string `xml:"key"`
} `xml:"identifierType"`
}, error) {
var results []struct {
IdentifierValue string `xml:"identifierValue"`
IdentifierType struct {
Label string `xml:"label"`
Summary string `xml:"summary"`
Key string `xml:"key"`
} `xml:"identifierType"`
}
decoder := xml.NewDecoder(strings.NewReader(xmlData))
for {
var item struct {
IdentifierValue string `xml:"identifierValue"`
IdentifierType struct {
Label string `xml:"label"`
Summary string `xml:"summary"`
Key string `xml:"key"`
} `xml:"identifierType"`
}
err := decoder.Decode(&item)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to decode HostSystemIdentificationInfo: %w", err)
}
results = append(results, item)
}
return results, nil
}⚠️ 注意事项:
-
不要尝试用 []T 直接 Unmarshal 多个根元素:即使类型定义为切片,xml.Unmarshal 仍要求输入 XML 有唯一根(如
),否则行为未定义。...> ...> - xml.Decoder 是流式解析器:它不依赖完整内存加载,适合处理大体积或动态生成的 XML 片段。
- 字段命名建议修正:原始代码中 IdentiferValue / IdentiferType 存在拼写错误(应为 IdentifierValue / IdentifierType),虽不影响解析,但建议统一修正以提升可维护性。
- xsi:type 属性可忽略:示例 XML 中的 xsi:type="HostSystemIdentificationInfo" 属于 SOAP 扩展属性,Go 的 encoding/xml 默认忽略未知属性,无需额外处理。
总结:面对多个并列 XML 元素,放弃 xml.Unmarshal,拥抱 xml.Decoder —— 这是 Go 标准库对“非标准 XML 片段”最稳健、最符合设计意图的处理方式。










