通八洲科技

如何在 Go 中正确反序列化 XML 多节点数组(解决仅解析首个元素的问题)

日期:2026-01-01 00:00 / 作者:碧海醫心

go 的 `xml.unmarshal` 默认只处理单个 xml 元素;当输入是多个并列的顶层 `` 节点时,必须使用 `xml.decoder` 循环调用 `decode` 才能完整解析全部项。

在 Go 中处理 VMware vSphere 等系统返回的 XML 数据时,一个常见陷阱是:XML 响应中包含多个同名、并列的顶层元素(如多个 ),而非包裹在统一父容器内。此时直接使用 xml.Unmarshal 会仅解析第一个节点,后续内容被忽略——这正是问题的根本原因。

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
}

关键要点说明:

⚠️ 注意事项:

通过采用 xml.Decoder 循环解码,即可稳定、准确地提取全部 HostSystemIdentificationInfo 实例,彻底解决“只拿到第一个元素”的问题。