1:import javax.xml.parsers.*; 2:import org.xml.sax.*; 3:import org.xml.sax.helpers.*; 4:import java.io.*; 5: 6:// A Simple SAX Application using JAXP with Namespace support 7:public class SaxMap{ 8: private SAXParserFactory factory; // Creates parser object 9: private SAXParser saxParser; // Holds a parser object 10: private DefaultHandler handler; // Defines the handler for this parser 11: 12: public SaxMap() throws SAXException{ 13: try{ 14: factory = SAXParserFactory.newInstance(); 15: factory.setNamespaceAware(true); 16: saxParser = factory.newSAXParser(); 17: 18: // Set Content Handlers 19: handler = new MyDefaultHandler(); 20: 21: } catch (ParserConfigurationException e){ 22: e.printStackTrace(); 23: } catch (SAXException e){ 24: e.printStackTrace(); 25: } 26: } 27: 28: public void parseDocument(String xmlFile){ 29: try{ 30: saxParser.parse(xmlFile, handler); 31: } catch (SAXException e){ 32: e.printStackTrace(); 33: } catch (IOException e){ 34: e.printStackTrace(); 35: } catch (Exception e){ 36: e.printStackTrace(); 37: } 38: } 39: 40: public static void main(String[] args){ 41: try { 42: if (args.length != 1) { 43: System.out.println( 44: "Usage: java SimpleJAXPns " + 45: "[XML Document Filename]"); 46: System.exit(0); 47: } 48: SaxMap xmlApp = new SaxMap(); 49: xmlApp.parseDocument(args[0]); 50: } catch (SAXException e){ 51: e.printStackTrace(); 52: } catch (Exception e) { 53: e.printStackTrace(); 54: } 55: } 56: 57: class MyDefaultHandler extends DefaultHandler{ 58: private CharArrayWriter buff = new CharArrayWriter(); 59: /* With a handler class, just override the methods you need to use 60: */ 61: public void startElement(String uri, String local, String qName, Attributes att){ 62: /* 63: When a start tag is found, reset the buffer. This clears out 64: any previous data we do not need and starts storing the text 65: for this element. 66: */ 67: buff.reset(); 68: } 69: 70: public void characters(char[] ch, int start, int length){ 71: // Store text in a buffer 72: buff.write(ch, start, length); 73: } 74: 75: public void endElement(String uri, String local, String qName){ 76: // Pick out the specific tags you are looking for 77: 78: if (local.equals("name")){ 79: System.out.println("Name: " + buff.toString()); 80: } 81: 82: if (local.equals("phone")){ 83: System.out.println("Phone #: " + buff.toString()); 84: } 85: } 86: 87: } 88:} 89: