xml - 尝试解码嵌套 XML 时获取所有字段

标签 xml go

这是我试图导入 Golang 的 XML 文件的一部分。实际的 XML 文件超过 500 MB。

<artists>
   <artist>
      <id>1</id>
      <name>The Persuader</name>
      <realname>Jesper Dahlbäck</realname>
      <profile />
   </artist>
    <artist>
       <id>22</id>
       <name>DATacide</name>
       <profile>Datacide began recording together in 1993, after Tetsu Inoue met Uwe Schmidt while vacationing near Frankfurt.
       </profile>
       <members>
          <id>25</id>
          <name>Tetsu Inoue</name>
          <id>519207</id>
          <name>Uwe Schmidt</name>
       </members>
    </artist>
</artists>

这是 Go 代码。我想获取 MEMBERS 部分中的所有 ID 字段,但我的代码仅获取最后一个 ID 字段,其中可能没有、一个或多个。如何将 MEMBERS 部分中的所有 ID 抓取到 MEMBERS 数组中?

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

type Artists struct {
    XMLName xml.Name `xml:"artists"`
    Artist  []Artist `xml:"artist"`
}

type Artist struct {
    XMLName xml.Name `xml:"artist"`
    ArtistID uint32 `xml:" id,omitempty"`
    ArtistName string `xml:" name,omitempty"`
    Profile string `xml:" profile,omitempty"`
    RealName string `xml:" realname,omitempty"`
    Members MembersID `xml:"members,omitempty"`
}

type MembersID struct {
    MemberID uint32 `xml:"id,omitempty"`
}

func main() {

    xmlFile, err := os.Open("short_artists.xml")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Successfully opened artists file")
    defer xmlFile.Close()

    byteValue, _ := ioutil.ReadAll(xmlFile)
    var artists Artists
    xml.Unmarshal(byteValue, &artists)

    for i := 0; i < len(artists.Artist); i++ {
        fmt.Println("ArtistID: " + fmt.Sprint(artists.Artist[i].ArtistID))
        fmt.Println("Name: " + artists.Artist[i].ArtistName)
        fmt.Println("Real Name: " + artists.Artist[i].RealName)
        fmt.Println("Profile: " + artists.Artist[i].Profile)
        fmt.Println("")
        fmt.Printf("%v\n",artists.Artist[i].Members)
        fmt.Println("")
    }
}

我所有的 Google 和 DuckDuckGo 搜索都是紫色的。感谢您的帮助。

最佳答案

问题是 MembersID 结构定义。你必须使用 slice 。

type MembersID struct {
    MemberID []uint32 `xml:"id,omitempty"`
}

播放链接:https://play.golang.org/p/h4qTmSQoRg

输出:

ArtistID: 1
Name: The Persuader
Real Name: Jesper Dahlbäck
Profile: 

Members: []

ArtistID: 22
Name: DATacide
Real Name: 
Profile: Datacide began recording together in 1993, after Tetsu Inoue met Uwe Schmidt while vacationing near Frankfurt.


Members: [25 519207]

额外提示:

如果需要,有选择地获取 XML 路径值。例如获取 XML 路径的所有 ID artist>members>id

type MemberID struct {
    IDs []uint32 `xml:"artist>members>id"`
}

播放链接:https://play.golang.org/p/sj7XPisgl7

输出:

[25 519207]

关于xml - 尝试解码嵌套 XML 时获取所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474394/

相关文章:

java - 获取 XML 文件的编码类型?

带有 Web 代理的 Golang Gorilla Websocket

go - go中出现 "internal error: failed to find embedded files of..."这样的错误信息是什么原因?

json - 将复杂的 JSON 结构初始化为 Golang 中的映射

android - 如何在 android 中使用 http post 发送 xml 文件。我将 xml 文件放在代码中的位置

xml - XSLT:属性内的 'xsl:value-of'

sql - ntext 到 xml 数据类型的 XML 数据

xml - SOAP::Data::Builder,当没有提供值时删除 xsi:nil ="true"

go - 从 go 获取 Windows 全名的最佳方法是什么?

json - 转到 AWS,从 DynamoDB 项目编码到 JSON,省略空值