首页  >  文章  >  后端开发  >  如何在 Golang 中检索 XML 数组中的所有元素而不仅限于第一个元素?

如何在 Golang 中检索 XML 数组中的所有元素而不仅限于第一个元素?

DDD
DDD原创
2024-10-24 06:02:30730浏览

How to Retrieve All Elements in an XML Array in Golang without Limiting to Just the First Element?

解组 XML 中的数组元素:检索所有元素,而不仅仅是第一个

在 Golang 中使用 xml.Unmarshal 解组 XML 数组时( []byte(p.Val.Inner), &t),您可能会遇到仅检索第一个元素的情况。要解决此问题,请利用 xml.Decoder 并重复调用其 Decode 方法。

解组所有 XML 数组元素的步骤:

  1. 创建一个新的 xml。使用 xml.NewDecoder(bytes.NewBufferString(VV)) 进行解码,其中 VV 是包含数组元素的 XML 字符串。
  2. 输入一个循环来处理每个 XML 元素:
  3. 声明一个变量 t目标切片类型(例如 HostSystemIdentificationInfo)。
  4. 调用 d.Decode(&t) 将下一个 XML 元素解组到 t 变量中。
  5. 重复步骤 2-4,直到 d.Decode(&t) 解组到 t 变量。 Decode(&t) 调用返回 io.EOF。

修改后的 Golang 代码:

<code class="go">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>`</code>

示例输出:

[{ unknown {Asset Tag Asset tag of the system AssetTag}}]
[{Dell System {OEM specific string OEM specific string OemSpecificString}}]
[{5[0000] {OEM specific string OEM specific string OemSpecificString}}]
[{REDACTED {Service tag Service tag of the system ServiceTag}}]

通过使用 xml.Decoder 并重复调用 Decode,可以成功检索出 XML 数组中的所有元素,解决了只能获取第一个元素的问题。

以上是如何在 Golang 中检索 XML 数组中的所有元素而不仅限于第一个元素?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn