"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Retrieve All Elements in an XML Array in Golang without Limiting to Just the First Element?

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

Published on 2024-11-09
Browse:170

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

Unmarshal Array Elements in XML: Retrieve All Elements, Not Just the First

When unmarshaling an XML array in Golang using xml.Unmarshal([]byte(p.Val.Inner), &t), you may encounter a situation where only the first element is being retrieved. To resolve this issue, utilize xml.Decoder and repeatedly invoke its Decode method.

Steps to Unmarshal All XML Array Elements:

  1. Create a new xml.Decoder using xml.NewDecoder(bytes.NewBufferString(VV)), where VV is the XML string containing the array elements.
  2. Enter a loop to process each XML element:
  3. Declare a variable t of the target slice type (e.g., HostSystemIdentificationInfo).
  4. Call d.Decode(&t) to unmarshal the next XML element into the t variable.
  5. Repeat steps 2-4 until the d.Decode(&t) call returns io.EOF.

Modified Golang Code:

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>`

Sample Output:

[{ 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}}]

By using xml.Decoder and repeatedly invoking Decode, you can successfully retrieve all elements within the XML array, resolving the issue of only obtaining the first element.

Release Statement This article is reprinted at: 1729695196 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3