MsgSAX.java.

Java To Go!
JMS in action
by Anthony Patton
Listing 1. MsgSAX.java.


//MsgSAX.java
     import org.xml.sax.*;
     import org.xml.sax.HandlerBase;
     import org.xml.sax.Parser;
     import org.xml.sax.Locator;
     import org.xml.sax.SAXException;
     import org.xml.sax.SAXParseException;
     import java.util.Hashtable;
1. public class MsgSAX extends HandlerBase {
            private boolean flag = false;
            private String comValue;;
            private Hashtable ht;
            private String currentCommodity;
2. public MsgSAX(int passedSize) {
                comValue = “”;
                flag = false;
                ht = new Hashtable();
                currentCommodity = “none”;
                ht = new Hashtable(passedSize); }
3. public Hashtable getValues() {
        return ht; }
        public void startDocument()
                throws SAXException { }
        public void endDocument()
                throws SAXException { }
4. public void startElement(String name, AttributeList amap)
            throws SAXException  {
5. if (name.equalsIgnoreCase(“quote”)) {
                currentCommodity =  amap.getValue(“request”);  }
            else if (name.equalsIgnoreCase(“recent”)) {
                flag = true;  } }
        public void endElement(String name)
            throws SAXException { }
6. public void characters(char[] ch, int start, int length)
            throws SAXException {
            if (flag == true) {
                ht.put(currentCommodity, new String(ch, start, length));
                flag = false;   } }
7. public void error(SAXParseException e) {
            ht.put(currentCommodity, “Error”); }
        public void fatalError(SAXParseException e) {
            ht.put(currentCommodity, “Error”); }
    } //: end of MsgSAX.java


Explanation
1. The class extends the SAX HandlerBase class. This is required to use the SAX approach.
2. This constructor accepts one parameter that sets the number of values to return; the number is used to create the Hashtable object. The Hashtable object stores the commodity symbol and value with the symbol as key.
3. The public getValues method returns a Hashtable object containing the commodity symbols and associated values.
4. The startElement method is called each time a new element is encountered. The sample XML from listing 1 is called nine times (quotes, quote, type, dispname, desc, month, year, market, and exch).
5. The code is concerned with the quote and recent elements only. The quote element contains the price for a commodity, and quote signals the beginning of a new commodity so the symbol is set via the request attribute of the quote element.
6. The characters method is called when text is encountered. Text is contained between element opening and closing tags. The boolean flag variable is set for recent (commodity value) elements. The flag is used for commodity retrieval and storage.
7. The commodity value is set if an error is encountered.

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.