在Golang中解组XML数组:只获取第一个元素

时间:2014-12-18 18:27:51

标签: 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结构而不是完整的数组。我该如何解决这个问题?

这是一个减少问题的游乐场:http://play.golang.org/p/5uRJ6Eu8jK

2 个答案:

答案 0 :(得分:4)

由于字符串中有多个顶级实体,因此必须创建一个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>`

答案 1 :(得分:2)

XML解析器需要具有单个顶级元素的格式良好的XML文档。它正在读取第一个元素,假设它是整个文档,然后停在那里。

HostSystemIdentificationInfo元素的父元素开始,然后解组:

<whatever>
    <HostSystemIdentificationInfo .../>
    <HostSystemIdentificationInfo .../>
    <HostSystemIdentificationInfo .../>
</whatever>

type HostSystemIdentificationInfo struct {
    IdentifierValue string 
    // ...
}

type whatever struct {
    Info []HostSystemIdentificationInfo `xml:"HostSystemIdentificationInfo"`
}

(如果需要,将XML包装在假的顶级元素中)。

请参阅Golang marshal dynamic xml element name后面的评论。