doushan6161 2017-08-03 03:42
浏览 41

尝试解组嵌套XML时抓住所有字段

Here is a bit of the XML file I'm trying to pull into Golang. The actual XML file is over 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>

Here's the Go code. I'm wanting to get ALL of the ID fields in the MEMBERS section, but my code is only grabbing the last ID field where there may be none, one, or many. How can I grab all IDs in the MEMBERS section into the MEMBERS array?

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
",artists.Artist[i].Members)
        fmt.Println("")
    }
}

All my Google and DuckDuckGo searches are purple. Thank you for your help.

  • 写回答

1条回答 默认 最新

  • dongniechi7825 2017-08-03 04:09
    关注

    Issue is MembersID struct definition. You have to use slice.

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

    Play link: https://play.golang.org/p/h4qTmSQoRg

    Output:

    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]
    

    Bonus Tip:

    Selectively fetching XML path values, if need be. For example getting all IDs of XML path artist>members>id

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

    Play link: https://play.golang.org/p/sj7XPisgl7

    Output:

    [25 519207]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题