MySAXApp.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // This is the QuickStart example from
  2. // http://www.saxproject.org/?selected=quickstart
  3. import java.io.FileReader;
  4. import org.xml.sax.XMLReader;
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.InputSource;
  7. import org.xml.sax.helpers.XMLReaderFactory;
  8. import org.xml.sax.helpers.DefaultHandler;
  9. public class MySAXApp extends DefaultHandler
  10. {
  11. public static void main (String args[])
  12. throws Exception
  13. {
  14. {
  15. XMLReader xr = XMLReaderFactory.createXMLReader("gnu.kawa.sax.KawaXMLReader");
  16. MySAXApp handler = new MySAXApp();
  17. xr.setContentHandler(handler);
  18. xr.setErrorHandler(handler);
  19. // Parse each file provided on the
  20. // command line.
  21. for (int i = 0; i < args.length; i++) {
  22. FileReader r = new FileReader(args[i]);
  23. xr.parse(new InputSource(r));
  24. }
  25. }
  26. }
  27. public MySAXApp ()
  28. {
  29. super();
  30. }
  31. ////////////////////////////////////////////////////////////////////
  32. // Event handlers.
  33. ////////////////////////////////////////////////////////////////////
  34. org.xml.sax.Locator locator;
  35. public void setDocumentLocator(org.xml.sax.Locator locator)
  36. {
  37. this.locator = locator;
  38. }
  39. public void startDocument ()
  40. {
  41. System.out.println("Start document");
  42. }
  43. public void endDocument ()
  44. {
  45. System.out.println("End document");
  46. }
  47. void printLineNumber ()
  48. {
  49. int line = -1, col = -1;
  50. if (locator != null)
  51. {
  52. line = locator.getLineNumber();
  53. col = locator.getColumnNumber();
  54. }
  55. if (line > 0)
  56. {
  57. System.out.print(line);
  58. if (col > 0)
  59. {
  60. System.out.print(':');
  61. System.out.print(col);
  62. }
  63. System.out.print(": ");
  64. }
  65. }
  66. public void startElement (String uri, String name,
  67. String qName, Attributes atts)
  68. {
  69. printLineNumber();
  70. if ("".equals (uri))
  71. System.out.println("Start element: " + qName);
  72. else
  73. System.out.println("Start element: {" + uri + "}" + name);
  74. }
  75. public void endElement (String uri, String name, String qName)
  76. {
  77. printLineNumber();
  78. if ("".equals (uri))
  79. System.out.println("End element: " + qName);
  80. else
  81. System.out.println("End element: {" + uri + "}" + name);
  82. }
  83. public void characters (char ch[], int start, int length)
  84. {
  85. printLineNumber();
  86. System.out.print("Characters: \"");
  87. for (int i = start; i < start + length; i++) {
  88. switch (ch[i]) {
  89. case '\\':
  90. System.out.print("\\\\");
  91. break;
  92. case '"':
  93. System.out.print("\\\"");
  94. break;
  95. case '\n':
  96. System.out.print("\\n");
  97. break;
  98. case '\r':
  99. System.out.print("\\r");
  100. break;
  101. case '\t':
  102. System.out.print("\\t");
  103. break;
  104. default:
  105. System.out.print(ch[i]);
  106. break;
  107. }
  108. }
  109. System.out.print("\"\n");
  110. }
  111. }