첫 번째 요소에만 제한하지 않고 Golang에서 XML 배열의 모든 요소를 ​​검색하는 방법은 무엇입니까?

DDD
풀어 주다: 2024-10-24 06:02:30
원래의
728명이 탐색했습니다.

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

XML에서 배열 요소 비정렬화: 첫 번째 요소뿐만 아니라 모든 요소 검색

xml.Unmarshal( []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. d가 나올 때까지 2~4단계를 반복합니다. 디코드(&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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!