MyXMLReader

来源:互联网 发布:余额宝怎么样知乎 编辑:程序博客网 时间:2024/04/29 19:23
  1. <?xml version="1.0" encoding="GB2312"?>
  2. <!-- 2003 年元月1 号长沙市各大影院落放映列表 -->
  3. <common city="ChangSha China" date="01/01/2003">
  4. <film name="英雄" price="30" station="田汉大剧场" time="19:00"
  5. describtion="国产最新大片,张艺谋导演,梁朝伟,张曼玉,李连杰等众多大明星主
  6. 演">
  7. </film>
  8. <film name="无间道" price="20" station="长沙市演艺中心" time="15:00"
  9. describtion="韩国大片">
  10. </film>
  11. <film name="武士" price="20" station="湖南省电影院" time="17:00"
  12. describtion="韩国大片,有点像英雄">
  13. </film>
  14. <film name="长排山之战" price="15" station="长沙市电影超市A1 厅" time="19:00"
  15. describtion="反映对越自卫反击战时期中国军人的故事片">
  16. </film>
  17. <film name="高山下的花环" price="15" station="长沙市电影超市A2 厅" time="19:00"
  18. describtion="反映对越自卫反击战时期中国军人的故事片">
  19. </film>
  20. <film name="这里的黎明静悄悄" price="15" station="长沙市电影超市A3 厅"
  21. time="19:00"
  22. describtion="反映对越自卫反击战时期中国军人的故事片">
  23. </film>
  24. <film name="子夜" price="15" station="长沙市电影超市B1 厅" time="19:00"
  25. describtion="反映对越自卫反击战时期中国军人的故事片">
  26. </film>
  27. </common>
 
  1. package src;
  2. import org.w3c.dom.*;
  3. import org.xml.sax.*;
  4. import org.xml.sax.helpers.*;
  5. import javax.xml.parsers.*;
  6. class MyXMLReader extends DefaultHandler {
  7.     // Fields
  8.     private int index;
  9.     private Locator locator;
  10.     // Constructor
  11.     public MyXMLReader() {
  12.         super(); // it must be done !
  13.     }
  14.     // nain method
  15.     public static void main(String[] args) {
  16.         try {
  17.             SAXParserFactory sf = SAXParserFactory.newInstance();
  18.             SAXParser sp = sf.newSAXParser();
  19.             MyXMLReader reader = new MyXMLReader();
  20.             sp.parse(new InputSource("film.xml"), reader);
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     // Response the startDocument event
  26.     public void startDocument() {
  27.         System.out
  28.                 .println("/n****************** (: 元旦电影海报 :) *****************************/n");
  29.     }
  30.     // Response the startElement event
  31.     /* (non-Javadoc)
  32.      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
  33.      */
  34.     /* (non-Javadoc)
  35.      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
  36.      */
  37.     public void startElement(String uri, String localName, String qName,
  38.             Attributes attrs) {
  39.         System.out.println(qName);
  40.         System.out.println(attrs);
  41.         System.out.println(uri);
  42.         System.out.println(localName);
  43.         System.out.println("###############");
  44.         if (qName.equalsIgnoreCase("film")) {
  45.             index++;
  46.             int attrCount = attrs.getLength();
  47.             for (int i = 0; i < attrCount; i++) {
  48.                 String attrName = attrs.getQName(i);
  49.                 if (attrName.equalsIgnoreCase("name")) {
  50.                     System.out.println("/t 第" + index + "场,片名:<<"
  51.                             + attrs.getValue(i) + ">>");
  52.                 }
  53.                 if (attrName.equalsIgnoreCase("price")) {
  54.                     System.out.println("/t 票价:" + attrs.getValue(i));
  55.                 }
  56.                 if (attrName.equalsIgnoreCase("station")) {
  57.                     System.out.println("/t 放映地点:" + attrs.getValue(i));
  58.                 }
  59.                 if (attrName.equalsIgnoreCase("time")) {
  60.                     System.out.println("/t 放映时间:" + attrs.getValue(i));
  61.                 }
  62.                 if (attrName.equalsIgnoreCase("describtion")) {
  63.                     System.out.println("/t 影片简介:" + attrs.getValue(i));
  64.                 }
  65.                 System.out.println(this.getLocator().getLineNumber());
  66.                 System.out.println("fdfdf");
  67.                 System.out.println();
  68.             }
  69.         }
  70.     }
  71.     // Response the endDocument event
  72.     public void endDocument() {
  73.         System.out.println("/t/t/t/t/t/t/t------ 共有" + index + "场电影要放映");
  74.     }
  75.     // Response the endElement event
  76.     public void endElement(String uri, String localName, String qName) {
  77.         // add your codes if neccessary ...
  78.     }
  79.     // Print the fata error information
  80.     public void fatalError(SAXParseException e) {
  81.         System.out.println("/nFatal error information -->");
  82.         System.out.println("/t" + e.getMessage());
  83.         System.out.println("/tAt line " + locator.getLineNumber() + ",column "
  84.                 + locator.getColumnNumber());
  85.     }
  86.     // Print the usual error information
  87.     public void error(SAXParseException e) {
  88.         System.out.println("/nUsual error information -->");
  89.         System.out.println("/t" + e.getMessage());
  90.         System.out.println("/tAt line " + locator.getLineNumber() + ",column "
  91.                 + locator.getColumnNumber());
  92.     }
  93.     // Print the warning information
  94.     public void warning(SAXParseException e) {
  95.         System.out.println("/nWarning information -->");
  96.         System.out.println("/t" + e.getMessage());
  97.         System.out.println("/tAt line " + locator.getLineNumber() + ",column "
  98.                 + locator.getColumnNumber());
  99.     }
  100.     // Store the error locator object
  101.     public void setDocumentLocator(Locator lct) {
  102.         locator = lct;
  103.     }
  104.     public Locator getLocator() {
  105.         return locator;
  106.     }
  107.     public void setLocator(Locator locator) {
  108.         this.locator = locator;
  109.     }
  110. }// End class MyXMLReader

 

 

原创粉丝点击