首頁  >  文章  >  後端開發  >  如何在 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}}]

範例輸出:

範例輸出:

以上是如何在 Golang 中檢索 XML 陣列中的所有元素而不僅限於第一個元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn