xml - 在 Golang 中解码 XML 数组 : Only Getting The First Element

标签 xml go

代码:

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)
            }
        }
    }

输出:

<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue> unknown</identifierValue><identifierType><label>Asset Tag</label><summary>Asset tag of the system</summary><key>AssetTag</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>Dell System</identifierValue><identifierType><label>OEM specific string</label><summary>OEM specific string</summary><key>OemSpecificString</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>5[0000]</identifierValue><identifierType><label>OEM specific string</label><summary>OEM specific string</summary><key>OemSpecificString</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>REDACTED</identifierValue><identifierType><label>Service tag</label><summary>Service tag of the system</summary><key>ServiceTag</key></identifierType></HostSystemIdentificationInfo>
[{ unknown {Asset Tag Asset tag of the system AssetTag}}]
<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue> unknown</identifierValue><identifierType><label>Asset Tag</label><summary>Asset tag of the system</summary><key>AssetTag</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>Dell System</identifierValue><identifierType><label>OEM specific string</label><summary>OEM specific string</summary><key>OemSpecificString</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>5[0000]</identifierValue><identifierType><label>OEM specific string</label><summary>OEM specific string</summary><key>OemSpecificString</key></identifierType></HostSystemIdentificationInfo><HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo"><identifierValue>REDCATED</identifierValue><identifierType><label>Service tag</label><summary>Service tag of the system</summary><key>ServiceTag</key></identifierType></HostSystemIdentificationInfo>
[{ unknown {Asset Tag Asset tag of the system AssetTag}}]

所以问题是,当我解码时,我只得到结果中的一个 HostSystemIdentification 结构,而不是整个数组。我该如何解决这个问题?

这是一个减少了问题的围棋 Playground :http://play.golang.org/p/5uRJ6Eu8jK

最佳答案

由于字符串中有多个顶级实体,因此必须创建一个 xml.Decoder 并多次调用其 Decode 方法。参见 http://play.golang.org/p/_1a77YGLoX

package main

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io"
    "log"
)

type HostSystemIdentificationInfo []struct {
    IdentiferValue string `xml:"identifierValue"`
    IdentiferType  struct {
        Label   string `xml:"label"`
        Summary string `xml:"summary"`
        Key     string `xml:"key"`
    } `xml:"identifierType"`
}

func main() {
    d := xml.NewDecoder(bytes.NewBufferString(VV))
    for {
        var t HostSystemIdentificationInfo
        err := d.Decode(&t)
        if err == io.EOF {
            break
        }
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(t)
    }
}

const VV = `<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo">
  <identifierValue> unknown</identifierValue>
  <identifierType>
    <label>Asset Tag</label>
    <summary>Asset tag of the system</summary>
    <key>AssetTag</key>
  </identifierType>
</HostSystemIdentificationInfo>
<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo">
  <identifierValue>Dell System</identifierValue>
  <identifierType>
    <label>OEM specific string</label>
    <summary>OEM specific string</summary>
    <key>OemSpecificString</key>
  </identifierType>
</HostSystemIdentificationInfo>
<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo">
  <identifierValue>5[0000]</identifierValue>
  <identifierType>
    <label>OEM specific string</label>
    <summary>OEM specific string</summary>
    <key>OemSpecificString</key>
  </identifierType>
</HostSystemIdentificationInfo>
<HostSystemIdentificationInfo xsi:type="HostSystemIdentificationInfo">
  <identifierValue>REDACTED</identifierValue>
  <identifierType>
    <label>Service tag</label>
    <summary>Service tag of the system</summary>
    <key>ServiceTag</key>
  </identifierType>
</HostSystemIdentificationInfo>`

关于xml - 在 Golang 中解码 XML 数组 : Only Getting The First Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553274/

相关文章:

android - 谷歌地图没有使用自定义按钮放大和缩小

Java 使用 DOM 解析 XML 响应

sql - Golang,postgresql rows.next() panic

等待 goroutine,但同时做一些事情

java - 在 Java 中创建 XML 文件的最佳方法是什么?

c# - 使用 C# 从 Magento 导入销售订单(salesOrderList 函数): "There is an error in XML document (2, 372)"

javascript - jQuery :contains() selector is not working IE

go - 如何读取 ReverseProxy 的响应正文

go - 关闭 chan 时出现死锁

go - Race(?) with Mutex - map 中的数据损坏