Thursday, July 22, 2010

Project 1 - Parsing the XML File

 Returning to Project 1: This class parsed the resulting XML file.

package iasearcher;
/**
 *
 */

/**
 * @author slittle2
 *
 *    This class contains code modified from http://www.exampledepot.com/egs/javax.xml.parsers/BasicSax.html
 *
 *   
 *
 */

import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class IASearcherXMLReader {
   
    private static int recordQuantity = 0;
    private static String identifiers = "";
    private static boolean grabCharacters = false;
    private static boolean failFlag = false;

    public static void main(String[] args) { // Create a handler to handle the SAX events generated during parsing
        parse();
    }

    // Method called by main or another class to parse an XML file
    public static void parse() {
       
        // Re-initialize variables -- necessary!
        recordQuantity = 0;
        identifiers = "";
        grabCharacters = false;
        failFlag = false;
       
        DefaultHandler handler = new XMLHandler(); // Parse the file using the handler
        parseXmlFile("C:/Documents and Settings/slittle2/workspace/MarcRetriever/output.xml", handler, false);
       
    }
   
    // To get the number of records found
    public static int getRecordQuantity(){
        return recordQuantity;
    }
   
    // To get the String of identifiers
    public static String getIdentifiers() {
        return identifiers;
    }
   
    // To return whether the parse succeeded
    public static boolean getFailFlag() {
        return failFlag;
    }
   
    // DefaultHandler contain no-op implementations for all SAX events.
    // This class should override methods to capture the events of interest.
    static class XMLHandler extends DefaultHandler {
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if(qName.equals("result")) { // "numFound" attribute is second, i.e. "1"
               
                recordQuantity = (Integer.valueOf(attributes.getValue("numFound"))).intValue();
            }

            try {
                if (attributes.getValue(0).equals("identifier")) {
                    grabCharacters = true;
                }
            } catch (NullPointerException npe) {
                // No attributes present! Move along!
            }
        }

        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if (grabCharacters) {
                identifiers += new String(ch, start, length) + "\t";
                grabCharacters = false;
            }
        }

    }
   
    // Parses an XML file using a SAX parser.
    // If validating is true, the contents is validated against the DTD
    // specified in the file.
    public static void parseXmlFile(String filename, DefaultHandler handler, boolean validating) {
        try { // Create a builder factory
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(validating); // Create the builder and parse the file
            factory.newSAXParser().parse(new File(filename), handler);
           
           
        } catch (SAXException e) { // A parsing error occurred; the xml input is not valid
            System.err.println("*** SAX Exception ***");
            failFlag = true;
        } catch (ParserConfigurationException e) {
            System.err.println("*** Parser Configuration Exception ***");
            failFlag = true;
        } catch (IOException e) {
            System.err.println("*** IO Exception ***");
            failFlag = true;
        } // End try-catch
    }
   
}

No comments:

Post a Comment