cvs: peardoc /en/package/fileformats/file-marc reading.xml

From: Date: Tue, 20 Feb 2007 01:49:53 +0000
Subject: cvs: peardoc /en/package/fileformats/file-marc reading.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9458@lists.php.net to get a copy of this message
dbs Tue Feb 20 01:49:53 2007 UTC Modified files: /peardoc/en/package/fileformats/file-marc reading.xml Log: Describe examples. Include coverage of field and subfield iterators. http://cvs.php.net/viewvc.cgi/peardoc/en/package/fileformats/file-marc/reading.xml?r1=1.1&r2=1.2&diff_format=u Index: peardoc/en/package/fileformats/file-marc/reading.xml diff -u peardoc/en/package/fileformats/file-marc/reading.xml:1.1 peardoc/en/package/fileformats/file-marc/reading.xml:1.2 --- peardoc/en/package/fileformats/file-marc/reading.xml:1.1 Mon Feb 12 22:10:53 2007 +++ peardoc/en/package/fileformats/file-marc/reading.xml Tue Feb 20 01:49:53 2007 @@ -1,9 +1,9 @@ -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.fileformats.file-marc.reading"> <refnamediv> - <refname>Introduction</refname> + <refname>Reading MARC data</refname> <refpurpose> - Introduction to <classname>File_MARC</classname> + Reading MARC data with <classname>File_MARC</classname> </refpurpose> </refnamediv> @@ -15,18 +15,36 @@ Readable Cataloging (<acronym>MARC</acronym>) data in MARC 21 format. <classname>File_MARCXML</classname>, which is bundled with <classname>File_MARC</classname>, allows you to read MARCXML formatted - data. Your input source can be a PHP stream (SOURCE_FILE) or a string - (SOURCE_STRING); specify the respective constant in the constructor. + data. + </para> + </refsect1> + + <refsect1 id="package.fileformats.file-marc.reading.source"> + <title>Reading MARC data from different sources</title> + <para> + Your input source can be a PHP stream (<constant>SOURCE_FILE</constant>) or a string + (<constant>SOURCE_STRING</constant>). The source location and source types are the + first and second arguments to the <classname>File_MARC</classname> + and <classname>File_MARCXML</classname> constructors. </para> <example> <title>Reading MARC 21 data from a file</title> + <para> + In the following example, MARC21 data has been stored on disk in a file + named <filename>journals.mrc</filename>. To read the MARCX21 data, we + call the constructor for <classname>File_MARC</classname>. We do not need + to tell File_MARC that <filename>journals.mrc</filename> is a filename or + stream pointing to the MARC content, because the value of the second + parameter of the constructor for <classname>File_MARC</classname> defaults + to <constant>SOURCE_FILE</constant>. + </para> <programlisting role="php"><![CDATA[ require 'File/MARC.php'; // Retrieve a set of MARC records from a file -$journals = new File_MARC('journals.mrc', SOURCE_FILE); +$journals = new File_MARC('journals.mrc'); // Iterate through the retrieved records while ($record = $journals->next()) { @@ -40,6 +58,15 @@ <example> <title>Reading MARCXML data from a string</title> + <para> + In the following example, MARCXML data has been returned from a call to + a Web service and has therefore been stored in a PHP variable, + <varname>$xml_data</varname>, as a string. To read the MARCXML data, we + call the constructor for <classname>File_MARCXML</classname>. To tell + File_MARCXML that <varname>$xml_data</varname> is a string, we specify + <constant>SOURCE_STRING</constant> as the second parameter of the + constructor. + </para> <programlisting role="php"><![CDATA[ require 'File/MARCXML.php'; @@ -56,7 +83,10 @@ ]]> </programlisting> </example> + </refsect1> + <refsect1 id="package.fileformats.file-marc.reading.structure"> + <title>Reading MARC data from different sources</title> <para> A <classname>File_MARC</classname> object consists of a leader and an iterable set of <classname>File_MARC_Record</classname> objects @@ -68,10 +98,45 @@ MARC subfields. </para> + <example> + <title>Printing the elements of a record</title> + + <programlisting role="php"><![CDATA[ +require 'File/MARC.php'; + +// Retrieve a set of MARC records +$bibrecords = new File_MARC('catdump.mrc', SOURCE_FILE); + +// Iterate through the retrieved records +while ($record = $bibrecords->next()) { + // Print the leader + print $record->getLeader(); + $subjects = $record->getFields('650'); + if ($subjects) { + // Retrieve just the first 24_ field + print $record->getField('24.', true); + print "\n"; + + // Now print all of the retrieved subjects + foreach ($subjects as $field) { + print $field; + print "\n"; + } + print "\n"; + } +} +]]> + </programlisting> + + </example> + + <para> - All of this means that <classname>File_MARC</classname> makes it very easy + All of this means that <classname>File_MARC</classname> makes it easy to read in a set of MARC records and iterate through the contents to retrieve - specific fields and subfields. <classname>File_MARC</classname> offers + specific fields and subfields. + + <classname>File_MARC</classname> offers convenience methods for retrieving specific fields without forcing you to iterate through the fields. <methodname>getField</methodname> returns the first field that matches the field name, while @@ -110,6 +175,54 @@ ]]> </programlisting> </example> + </refsect1> + + <refsect1 id="package.fileformats.file-marc.reading.iterating"> + <title>Iterating through fields and subfields</title> + <para> + When you iterate over a <classname>File_MARC_DataField</classname> object + using <function>foreach</function>, the MARC tag for the given field is + returned as the key for the element and the set of subfields is returned + as the value of the element. + </para> + <para> + Similarly, when you iterate over a <classname>File_MARC_Subfield</classname> + object using <function>foreach</function>, the code for the given subfield + is returned as the key of the element and the value of the given subfield + is returned as the value of the element. + </para> + + <example> + <title>Iterating over fields and subfields in a MARC record</title> + <para> + In the following example, we iterate through a set of 650 fields + to print out the subject headings contained in the subfields for each + field. + </para> + <programlisting role="php"><![CDATA[ +require 'File/MARC.php'; + +// Retrieve a set of MARC records from a z39 result string +$bibrecords = new File_MARC($z39_result, SOURCE_STRING); + +// Go through each record +while ($record = $bibrecords->next()) { + // Iterate through the fields + foreach ($record as $tag => $subfields) { + // Skip everything except for 650 fields + if ($tag == '650') { + print "Subject:"; + foreach ($subfields as $code => $value) { + print " $value"; + } + print "\n"; + } + } +} +]]> + </programlisting> + + </example> </refsect1>

« previous php.pear.doc (#9458) next »