marcxml

package
v0.33.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package marcxml reads and writes MARC 21 records in the Library of Congress MARCXML "slim" serialization (namespace http://www.loc.gov/MARC21/slim), implementing codex.RecordReader and codex.RecordWriter using only encoding/xml.

A MARCXML document is a <collection> of <record> elements, each holding a <leader>, <controlfield> elements (tags below "010") and <datafield> elements carrying two indicator attributes and <subfield> children. Values are UTF-8 and are XML-escaped on write. Decoding is namespace-agnostic, so documents with or without the slim namespace (and a bare <record> with no <collection> wrapper) are accepted.

Index

Constants

View Source
const Namespace = "http://www.loc.gov/MARC21/slim"

Namespace is the MARCXML slim schema namespace.

Variables

This section is empty.

Functions

func Decode

func Decode(b []byte) (*codex.Record, error)

Decode parses a single record from a MARCXML byte slice (a <record>, or the first record of a <collection>).

func Encode

func Encode(r *codex.Record) ([]byte, error)

Encode serializes a record as a standalone MARCXML <record> element, indented and carrying the slim namespace declaration. Use a Writer to emit a full <collection> document.

func ReadFile

func ReadFile(path string) ([]*codex.Record, error)

ReadFile reads every record from the named MARCXML file. On the first malformed record it returns the records parsed so far together with the error.

func WriteFile

func WriteFile(path string, records []*codex.Record) error

WriteFile writes every record to the named file as a MARCXML collection, creating it or truncating an existing file.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads MARC records from a MARCXML stream one <record> at a time. It does not buffer the whole document.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a Reader that reads records from r.

func (*Reader) All

func (rd *Reader) All() iter.Seq2[*codex.Record, error]

All returns an iterator over the remaining records in the stream, for use as "for rec, err := range r.All()". It stops at the first error.

func (*Reader) Read

func (rd *Reader) Read() (*codex.Record, error)

Read returns the next record, or io.EOF when the stream is exhausted. It advances to the next <record> element (inside a <collection> or at the top level) and decodes it by walking tokens, so field order is preserved exactly and the reflection cost of struct unmarshaling is avoided.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer streams records into a MARCXML <collection>. Close must be called to emit the closing </collection> and complete the document.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a Writer that writes a MARCXML collection to w.

func (*Writer) Close

func (wr *Writer) Close() error

Close writes the closing </collection> tag. It must be called to produce a complete document; an empty collection is emitted if no records were written.

func (*Writer) Write

func (wr *Writer) Write(r *codex.Record) error

Write serializes one record as a <record> element within the collection, emitting the document header and <collection> open tag before the first record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL