1 4 import java.io.FileReader ; 5 6 import org.xml.sax.XMLReader ; 7 import org.xml.sax.Attributes ; 8 import org.xml.sax.InputSource ; 9 import org.xml.sax.helpers.XMLReaderFactory ; 10 import org.xml.sax.helpers.DefaultHandler ; 11 12 13 public class MySAXApp extends DefaultHandler 14 { 15 16 public static void main (String args[]) 17 throws Exception 18 { 19 { 20 XMLReader xr = XMLReaderFactory.createXMLReader("gnu.kawa.sax.KawaXMLReader"); 21 MySAXApp handler = new MySAXApp(); 22 xr.setContentHandler(handler); 23 xr.setErrorHandler(handler); 24 25 for (int i = 0; i < args.length; i++) { 28 FileReader r = new FileReader (args[i]); 29 xr.parse(new InputSource (r)); 30 } 31 } 32 } 33 34 35 public MySAXApp () 36 { 37 super(); 38 } 39 40 41 45 46 public void startDocument () 47 { 48 System.out.println("Start document"); 49 } 50 51 52 public void endDocument () 53 { 54 System.out.println("End document"); 55 } 56 57 58 public void startElement (String uri, String name, 59 String qName, Attributes atts) 60 { 61 if ("".equals (uri)) 62 System.out.println("Start element: " + qName); 63 else 64 System.out.println("Start element: {" + uri + "}" + name); 65 } 66 67 68 public void endElement (String uri, String name, String qName) 69 { 70 if ("".equals (uri)) 71 System.out.println("End element: " + qName); 72 else 73 System.out.println("End element: {" + uri + "}" + name); 74 } 75 76 77 public void characters (char ch[], int start, int length) 78 { 79 System.out.print("Characters: \""); 80 for (int i = start; i < start + length; i++) { 81 switch (ch[i]) { 82 case '\\': 83 System.out.print("\\\\"); 84 break; 85 case '"': 86 System.out.print("\\\""); 87 break; 88 case '\n': 89 System.out.print("\\n"); 90 break; 91 case '\r': 92 System.out.print("\\r"); 93 break; 94 case '\t': 95 System.out.print("\\t"); 96 break; 97 default: 98 System.out.print(ch[i]); 99 break; 100 } 101 } 102 System.out.print("\"\n"); 103 } 104 105 } 106 | Popular Tags |