duanganleng0577 2019-09-15 22:13
浏览 58

使用Go解析XML文件

I'm new on Go. I have a task to do where I have a xml file with the following structure. There many other tag record in this file.

I need to read all the records, and print the information about the Kind of music but only where datafield tag is equal "650" and the subfield code tag = "a".

<record>
  <leader>01153cjm a22002771a 4500</leader>
  <controlfield tag="001">   00000838 </controlfield>
  <controlfield tag="003">DLC</controlfield>
  <controlfield tag="005">20030506181700.0</controlfield>
  <controlfield tag="007">sd|zsngnnmmned</controlfield>
  <controlfield tag="008">000824s1998    nyuppn                  d</controlfield>
  <datafield tag="050" ind1="0" ind2="0">
    <subfield code="a">SDA 16949</subfield>
  </datafield>
  <datafield tag="010" ind1=" " ind2=" ">
    <subfield code="a">   00000838 </subfield>
  </datafield>
  <datafield tag="020" ind1=" " ind2=" ">
    <subfield code="c">$15.98</subfield>
  </datafield>
  <datafield tag="024" ind1="1" ind2=" ">
    <subfield code="a">601215312621</subfield>
  </datafield>
  <datafield tag="650" ind1=" " ind2="0">
    <subfield code="a">Rap (Music)</subfield>
  </datafield>
</record>

My code is:

package main

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

type Record struct {
    XMLName      xml.Name `xml:"record"`
    Text         string   `xml:",chardata"`
    Leader       string   `xml:"leader"`
    Controlfield []struct {
        Text string `xml:",chardata"`
        Tag  string `xml:"tag,attr"`
    } `xml:"controlfield"`
    Datafield []struct {
        Text     string `xml:",chardata"`
        Tag      string `xml:"tag,attr"`
        Ind1     string `xml:"ind1,attr"`
        Ind2     string `xml:"ind2,attr"`
        Subfield []struct {
            Text string `xml:",chardata"`
            Code string `xml:"code,attr"`
        } `xml:"subfield"`
    } `xml:"datafield"`
}

func main() {

    xmlFile, err := os.Open("music_lite3.xml")

    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Successfully Opened music_lite3.xml")

    defer xmlFile.Close()

    data, _ := ioutil.ReadAll(xmlFile)

    var record Record

    xml.Unmarshal(data, &record)

    for j := 0; j < len(record.Controlfield); j++ {
        for i := 0; i < len(record.Datafield); i++ {
            if record.Datafield == "650" {
                if record.Datafield.Subfield.Code == "a" {
                    fmt.Println("Kind of Music: " + record.Datafield.Subfield.Text)
                }
            }
        }
    }
}

I'm having problems to acess the structs datafield and subfield. It seems that when a call record.Datafield and record.Datafield.Subfield.Code Go is not recognizing the structs.

Anyone can help me? Thanks in advance.

=)

  • 写回答

1条回答 默认 最新

  • dongqi6964 2019-09-15 22:30
    关注

    Your code doesn't even compile. You need to index the slices inside the loops, you can't refer to fields of elements of slices without indexing them. Also Record.Controlfield and Record.Datafield are not "related", it's senseless to process them with embedded loops.

    However, you do need to use nested loops to check Subfield of a Datafield whose tag matches.

    Something like this:

    for i := range record.Datafield {
        df := &record.Datafield[i]
        if df.Tag == "650" {
            for j := range df.Subfield {
                if df.Subfield[j].Code == "a" {
                    fmt.Println("Kind of Music: " + df.Subfield[j].Text)
                }
            }
        }
    }
    

    Try it on the Go Playground.

    Note that you could use for range to also "get" the element at the given index, but since you have some kind of generated struct which uses anonymous structs (and not pointers), such a loop would copy all elements (hence I stayed with "manual indexing").

    Also do check all errors, e.g. ioutil.ReadAll(xmlFile) and xml.Unmarshal(data, &record).

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?