Skip to content

Instantly share code, notes, and snippets.

@eliotjordan
Last active July 2, 2019 19:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliotjordan/dcfb5523a49c2edbba3c77e4ad383125 to your computer and use it in GitHub Desktop.
Save eliotjordan/dcfb5523a49c2edbba3c77e4ad383125 to your computer and use it in GitHub Desktop.
Process MARC XML with golang
package main
import (
"encoding/xml"
"fmt"
"strings"
"time"
"io/ioutil"
"os"
)
type Collection struct {
Records []Record `xml:"record"`
}
type Record struct {
Controlfields []Controlfield `xml:"controlfield"`
Datafields []Datafield `xml:"datafield"`
}
type Controlfield struct {
Tag string `xml:"tag,attr"`
Value string `xml:",chardata"`
}
type Datafield struct {
Tag string `xml:"tag,attr"`
Subfields []Subfield `xml:"subfield"`
}
type Subfield struct {
Code string `xml:"code,attr"`
Value string `xml:",chardata"`
}
func controlfield_value(record Record, tag string) (value string) {
for i := 0; i < len(record.Controlfields); i++ {
if record.Controlfields[i].Tag == tag {
value = record.Controlfields[i].Value
}
}
return value
}
func datafield_value(record Record, tag string, codes []string) (value string) {
for i := 0; i < len(record.Datafields); i++ {
if record.Datafields[i].Tag == tag {
value = concat_subfields(record.Datafields[i].Subfields, codes)
}
}
return value
}
func concat_subfields(subfields []Subfield, codes []string) (string) {
var values []string
for i :=0; i < len(subfields); i++ {
var subfield Subfield
subfield = subfields[i]
if stringInSlice(subfield.Code, codes) {
values = append(values, subfield.Value)
}
}
return strings.Join(values[:], " ")
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func main() {
start := time.Now()
xmlFile, err := os.Open("1520931620")
if err != nil {
fmt.Println(err)
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
var marc Collection
xml.Unmarshal(byteValue , &marc)
for i := 0; i < len(marc.Records); i++ {
var record Record
record = marc.Records[i]
// fmt.Println(controlfield_value(record, "001"))
// fmt.Println(datafield_value(record, "245", []string {"a","b","c","f","g","h","k","n","p","s"}))
controlfield_value(record, "001")
datafield_value(record, "245", []string {"a","b","c","f","g","h","k","n","p","s"})
}
fmt.Println(time.Since(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment