/** * Very simple SAX parser. Parses the file whose name is provided * as an argument to the main method. For that file, the start * and end element events are reported, as well as the element * character content is printed. * * @author Nauman Chaudhry * 04/09/07 */ import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class Ex1BasicHandler extends DefaultHandler { public static void main(String[] args){ try{ XMLReader xmlReader = XMLReaderFactory.createXMLReader(); Ex1BasicHandler handler = new Ex1BasicHandler(); xmlReader.setContentHandler(handler); xmlReader.parse(args[0]); }catch(Exception e){ e.printStackTrace(); } } public void startDocument() throws SAXException{ System.out.println("Start document"); } public void endDocument() throws SAXException{ System.out.println("End document"); } public void startElement(String namespaceURI, String localName, String qualName, Attributes atts){ System.out.println("startElement:\t" + qualName); } public void endElement(String namespaceURI, String localName, String qualName){ System.out.println("endElement:\t" + qualName); } public void characters(char[] charData, int start, int length) throws SAXException { System.out.println("content:\t" + new String(charData, start, length)); } }