5.5.4 Sample Program (SampleSAX.java)
The following shows the sample program:
import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import org.xml.sax.SAXException; import java.io.*; public class SampleSAX{ public static final void main(String[] args){ try{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); String xml = "SampleSAX.xml"; DefaultHandler handler = new EventHandler(); parser.parse(xml, handler); }catch(ParserConfigurationException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(SAXException e){ e.printStackTrace(); } } } //calculate an amount of money in total and output it class EventHandler extends DefaultHandler{ int tvPrice; //Television unit price int radioPrice; //Radio unit price int videoPrice; //Video unit price int tvNum; //Television amount int radioNum; //Radio amount int videoNum; //Video amount boolean tvFlag; //Television flag boolean radioFlag; //Radio flag boolean videoFlag; //Video flag boolean tankaFlag; //Unit price flag boolean volFlag; //Amount flag boolean nameFlag; //Brand Name flag String str; //Character string of element contents StringBuffer buffer; //buffer for character string connections of element contents //start notice event of XML public void startDocument(){ tvPrice = 0; radioPrice = 0; videoPrice = 0; tvNum = 0; radioNum = 0; videoNum = 0; tvFlag = false; radioFlag = false; videoFlag = false; tankaFlag = false; volFlag = false; nameFlag = false; } //end notice event of XML public void endDocument(){ int sum = tvNum * tvPrice + radioNum * radioPrice + videoNum * videoPrice; System.out.println(sum + " yen in total."); } //element start (a start of a tag) notice event public void startElement(String nameSpace, String localName, String modName, Attributes attr){ buffer = new StringBuffer(); if(modName.equals("Unit_Price")){ tankaFlag = true; } if(modName.equals("Amount")){ volFlag = true; } if(modName.equals("Brand_Name")){ nameFlag = true; } } //element end (the end of a tag) notice event public void endElement(String nameSpace, String localName, String modName){ str = buffer.toString(); if(nameFlag == true){ if(str.equals("Television")){ tvFlag = true; } if(str.equals("Radio")){ radioFlag = true; } if(str.equals("Video")){ videoFlag = true; } }else if(tvFlag == true){ if(tankaFlag == true){ //set unit price of television tvPrice = Integer.parseInt(str); }else if(volFlag == true){ //set amount of television tvNum = Integer.parseInt(str); } }else if(radioFlag == true){ if(tankaFlag == true){ //set unit price of radio radioPrice = Integer.parseInt(str); }else if(volFlag == true){ //set amount of radio radioNum = Integer.parseInt(str); } }else if(videoFlag == true){ if(tankaFlag == true){ //set unit price of video videoPrice = Integer.parseInt(str); }else if(volFlag == true){ //set amount of video videoNum = Integer.parseInt(str); } } if(modName.equals("Unit_Price")){ tankaFlag = false; } if(modName.equals("Amount")){ volFlag = false; } if(modName.equals("Brand_Name")){ nameFlag = false; } if(modName.equals("Merchandise")){ tvFlag = false; radioFlag = false; videoFlag = false; } } //notice of character string event public void characters(char[] ch, int start, int length){ buffer.append(ch, start, length); } }