go 的 `xml.unmarshal` 默认只处理单个 xml 元素;当输入是多个并列的顶层 `
在 Go 中处理 VMware vSphere 等系统返回的 XML 数据时,一个常见陷阱是:XML 响应中包含多个同名、并列的顶层元素(如多个
xml.Unmarshal 设计上期望输入为「单个完整 XML 文档或元素」,它无法自动识别并跳转到下一个同级标签。要正确解析这种“多根节点”格式(严格来说不符合标准 XML 文档结构,但常见于 SOAP 或某些 API 响应),必须改用流式解析器 xml.Decoder:
import (
"bytes"
"encoding/xml"
"io"
)
type HostSystemIdentificationInfo struct { // 注意:此处改为单个结构体,更清晰且便于解码
IdentifierValue string `xml:"identifierValue"`
IdentifierType struct {
Label string `xml:"label"`
Summary string `xml:"summary"`
Key string `xml:"key"`
} `xml:"identifierType"`
}
// 解析多个并列的 HostSystemIdentificationInfo 节点
func parseMultipleXML(xmlData string) ([]HostSystemIdentificationInfo, error) {
var results []HostSystemIdentificationInfo
decoder := xml.NewDecoder(bytes.NewBufferString(xmlData))
for {
var item HostSystemIdentificationInfo
err := decoder.Decode(&item)
if err == io.EOF {
break // 所有节点已读取完毕
}
if err != nil {
return nil, err // 如遇格式错误(如标签不闭合),立即返回
}
results = append(results, item)
}
return results, nil
}✅ 关键要点说明:
⚠️ 注意事项:
ode 可能提前失败; 通过采用 xml.Decoder 循环解码,即可稳定、准确地提取全部 HostSystemIdentificationInfo 实例,彻底解决“只拿到第一个元素”的问题。