0
を参照してくださいコード:Golangアンマーシャリング変化構造インデクス定義された値に
印刷アウトv.Src [0]とv.Srcは、[1] "MYSOURCE" と "MySource2" を起動します。
しかし、XMLを比較し、エントリ[0]と[1]私は、デコーダは、インデックスとして<id>x</id>
を使用していることを達成することができますどのように
<id>x</id>
のidのセットに従わないのですか?
対象:ここv.Src [1]版画 "MYSOURCE"
が私の作業コードがメイン
パッケージのすべての助けを
import (
"encoding/xml"
"fmt"
)
type Flow struct {
Id string `xml:"id"`
Name string `xml:"name"`
}
type Src struct {
Id string `xml:"id"`
Name string `xml:"name"`
Flows []Flow `xml:"flows>flow"`
}
type Result struct {
Src []Src `xml:"bar>sources>source"`
}
func main() {
data := `
<foo>
<bar>
<sources>
<source>
<id>1</id>
<name>MySource</name>
<flows>
<flow>
<id>1</id>
<name>MySource 1L</name>
</flow>
<flow>
<id>2</id>
<name>MySource 1R</name>
</flow>
</flows>
</source>
<source>
<id>2</id>
<name>MySource2</name>
<flows>
<flow>
<id>1</id>
<name>MySource2 2L</name>
</flow>
<flow>
<id>2</id>
<name>MySource2 2R</name>
</flow>
</flows>
</source>
</sources>
</bar>
</foo>`
v := Result{}
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("%#v", v)
fmt.Printf("%#v", v.Src[0].Name) //Prints: "MySource"
fmt.Printf("%#v", v.Src[1].Name) //Prints: "MySource2"
}
感謝です!