ホームページ  >  記事  >  バックエンド開発  >  最初の要素だけに限定せずに、Golang で XML 配列内のすべての要素を取得する方法

最初の要素だけに限定せずに、Golang で XML 配列内のすべての要素を取得する方法

DDD
DDDオリジナル
2024-10-24 06:02:30732ブラウズ

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

XML の配列要素のアンマーシャリング: 最初の要素だけでなくすべての要素を取得

xml.Unmarshal( を使用して Golang で XML 配列をアンマーシャリングする場合[]byte(p.Val.Inner), &t) を使用すると、最初の要素のみが取得される状況が発生する可能性があります。この問題を解決するには、xml.Decoder を利用し、その Decode メソッドを繰り返し呼び出します。

すべての XML 配列要素をアンマーシャルする手順:

  1. 新しい xml を作成します。 xml.NewDecoder(bytes.NewBufferString(VV)) を使用したデコーダー。VV は配列要素を含む XML 文字列です。
  2. 各 XML 要素を処理するループを入力します。
  3. 変数 t を宣言します。
  4. d.Decode(&t) を呼び出して、次の XML 要素を t 変数にアンマーシャリングします。
  5. d になるまで手順 2 ~ 4 を繰り返します。 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。