Interfaces for the record object model.

The goal of the org.marc4j.marc package is to provide a clean and simple to use interface to create, store and edit MARC records or parts of records as objects. See the examples below to get started using the object model.

Reading records

The {@link org.marc4j.marc.Record} interface provides access to the leader and variable fields for each record returned by the {@link org.marc4j.MarcReader} implementation.

The following example retrieves all control fields (tags 001 through 009):

    List fields = record.getControlFields();
    

This method retuns the fields as a {@link java.util.List}, thus enabling the use of the standard Java collections framework to iterate over collections of records, fields or subfields. The following code snippet prints the tag and data for each control field to standard output:

    Iterator i = fields.iterator();
    while (i.hasNext()) {
    	ControlField field = (ControlField) i.next();
    	System.out.println("Tag: " + field.getTag() + " Data: " + field.getData());
    }
    

The getDataFields() method returns all data fields (tags 010 through 999). An {@link org.marc4j.marc.DataField} provides access to the tag, the indicators and the subfields. For example to write all the data field information to standard output:

    List fields = record.getDataFields();
    Iterator i = fields.iterator();
    while (i.hasNext()) {
        DataField field = (ControlField) i.next();
        System.out.println("Tag: " + field.getTag() + " ind1: " + field.getIndicator1() + 
            " ind2: " + field.getIndicator2());
        List subfields = field.getSubfields();
        Iterator j = subfields.iterator();
        while (j.hasNext()) {
            Subfield subfield = (Subfield) j.next();
            System.out.println("Subfield code: " + subfield.getCode() + 
                "Data: " + subfield.getData());
        }
    }
    

If you want to retrieve specific fields you can use one of the following methods:

    // get the first field occurence for a given tag
    DataField title = (DataField)record.getVariableField("245");
    
    // get all occurences for a particular tag
    List subjects = record.getVariableFields("650");
    
    // get all occurences for a given list of tags
    String[] tags = {"010", "100", "245", "250", "260", "300"};
    List fields = record.getVariableFields(tags);
    

In addition you can use simple searches using the find() methods to retrieve fields that meet certain criteria. The search capabilities are very limited, but they can be useful when processing records. The following code snippet provides some examples:

    // find any field containing 'Chabon'
    List fields = record.find("Chabon");
    
    // find 'Summerland' in a title field
    List fields = record.find("245", "Summerland");
    
    // find 'Graham, Paul' in main or added entries for a personal name:
    String tags = {"100", "600"};
    List fields = record.find(tags, "Graham, Paul")  
    

The find method is also useful if you want to retrieve records that meet certain criteria, such as a specific control number, title words or a particular publisher or subject. The example below checks if the cataloging agency is DLC. The example also shows how you can extend the find capailities to specific subfields, a feature not directly available in MARC4J, since it is easy to accomplish using the record model together with the standard Java API's.

    InputStream input = new FileInputStream("file.mrc");
    MarcReader reader = new MarcStreamReader(input);
    while (reader.hasNext()) {
    	Record record = reader.next();
    	
    	// check if the cataloging agency is DLC
    	List result = record.find("040", "DLC");
    	if (result.size() > 0)
            System.out.println("Agency for this record is DLC");
    	
    	// there is no specific find for a specific subfield
    	// so to check if it is the orignal cataloging agency
    	DataField field = (DataField)result.get(0);
    	String agency = field.getSubfield('a').getData();
    	if (agency.matches("DLC"))
            System.out.println("DLC is the original agency");    	
    	
    }
    

By using find() you can also implement a kind of search and replace to batch update records that meet certain criteria. You can use Java regular expressions in find() methods. Check the java.util.regex package for more information and examples.

Creating or updating records

You can also create or update records using the {@link org.marc4j.marc.MarcFactory}. For example:

    // create a factory instance
    MarcFactory factory = MarcFactory.newInstance();
	
    // create a record with leader
    Record record = factory.newRecord("00000cam a2200000 a 4500");
	
    // add a control field
    record.addVariableField(factory.newControlField("001", "12883376"));
	
    // add a data field
    DataField df = factory.newDataField("245", '1', '0');
    df.addSubfield(factory.newSubfield('a', "Summerland /"));
    df.addSubfield(factory.newSubfield('c', "Michael Chabon."));
    record.addVariableField(df);
    

You can use a {@link org.marc4j.MarcWriter} implementation to serialize your records for example to MARC or MARC XML. The code snippet below writes a single record in MARC format to standard output:

    MarcWriter writer = new MarcStreamWriter(System.out);
    writer.write(record);
    writer.close(); 
    

Check the Javadoc for {@link org.marc4j.MarcStreamWriter} and {@link org.marc4j.MarcXmlWriter} for more information.