gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:39:34 25 4
gpt4 key购买 nike

代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com