Thursday, June 9, 2011

XML Parsing With SAXParser


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class LocalDataObject {

    private int noOfFields;
    private Vector recordValues = null;
    private int tblCode = 0;
    private String tblName;
    private static SAXParserFactory factory = null;
    private SAXParser saxParser = null;

    public LocalDataObject(int tblCode) {
        this.tblCode = tblCode;
        tblName = ConstantCodes.TBL_NAMES[tblCode];
        this.noOfFields = ConstantCodes.FLD_NAMES[this.tblCode].length;
        recordValues = new Vector();
        recordValues.setSize(noOfFields);

    }

    public LocalDataObject(int tblCode, String socId) {
        this(tblCode);
        if (tblCode == ConstantCodes.TBL_CUSTOMERDETAILS_CODE) {
            tblName = ConstantCodes.TBL_NAMES[ConstantCodes.TBL_CUSTOMERDETAILS_CODE];
        }
        tblName += socId;
    }

    public void setValue(int fieldIndex, String value) {
        if (fieldIndex < noOfFields) {
            recordValues.setElementAt(value, fieldIndex);
        }
    }

    public String getValue(int fieldValue) {
        return (String) recordValues.elementAt(fieldValue);
    }

    public boolean loadDataFromID(int rmsID) {
        RMSStore tempStore = new RMSStore();
        tempStore.openRecordStore(tblName);

        boolean dataLoaded = false;

        try {
            String xmlString = new String(tempStore.getRecordByteFromId(rmsID));
            if (xmlString != null) {
                dataLoaded = setValuesFromXML(xmlString);
            } else {
                dataLoaded = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            dataLoaded = false;
        } finally {
            tempStore.closeRecordStore();
        }
        return dataLoaded;
    }

    static {
        try {
            factory = SAXParserFactory.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean setValuesFromXML(String xml) {
        InputStream is = null;
        try {
            XMLHandler handler = new XMLHandler();
            handler.setParameters(noOfFields, recordValues);
            is = new ByteArrayInputStream(xml.getBytes());
            InputSource inputSource = new InputSource(is);
            saxParser = factory.newSAXParser();
            saxParser.parse(inputSource, handler);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer();

        buffer.append("<ROW>");
        for (int i = 0; i < noOfFields; i++) {

            buffer.append("<");
            buffer.append(ConstantCodes.FLD_NAMES[tblCode][i]);
            buffer.append(">");

            buffer.append(getValue(i));

            buffer.append("</");
            buffer.append(ConstantCodes.FLD_NAMES[tblCode][i]);
            buffer.append(">");
        }
        buffer.append("</ROW>");

        return buffer.toString();
    }

    public static Vector getXMLRecords(String xml) {
        String record = null;
        String rec = "</ROW>";
        Vector vRec = new Vector();
        int index = 0;
        while (true) {
            index = xml.indexOf(rec);
            if (index == -1) {
                break;
            }
            record = xml.substring(0, index + rec.length());
            vRec.addElement(record);
            xml = xml.substring(record.length());
        }
        record = null;

        return vRec;
    }


    static class XMLHandler extends DefaultHandler {

        Vector xmlRecord = null;
        int count = 0;
        int noOfFields;

        private void setParameters(int noOfFields, Vector recordValues) {
            this.noOfFields = noOfFields;
            this.xmlRecord = recordValues;
        }

        public void startDocument() throws SAXException {

            xmlRecord.removeAllElements();
            xmlRecord.setSize(noOfFields);
        }

        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
        }

        public void characters(char[] ch, int start, int length)
                throws SAXException {

            xmlRecord.setElementAt(new String(ch, start, length).trim(), count);
            count += 1;
        }

        public void endElement(String uri, String localName, String qName)
                throws SAXException {
        }

        public void endDocument() throws SAXException {
        }
    }
}

No comments:

Post a Comment