MyXMLReader
- <?xml version="1.0" encoding="GB2312"?>
- <!-- 2003 年元月1 号长沙市各大影院落放映列表 -->
- <common city="ChangSha China" date="01/01/2003">
- <film name="英雄" price="30" station="田汉大剧场" time="19:00"
- describtion="国产最新大片,张艺谋导演,梁朝伟,张曼玉,李连杰等众多大明星主
- 演">
- </film>
- <film name="无间道" price="20" station="长沙市演艺中心" time="15:00"
- describtion="韩国大片">
- </film>
- <film name="武士" price="20" station="湖南省电影院" time="17:00"
- describtion="韩国大片,有点像英雄">
- </film>
- <film name="长排山之战" price="15" station="长沙市电影超市A1 厅" time="19:00"
- describtion="反映对越自卫反击战时期中国军人的故事片">
- </film>
- <film name="高山下的花环" price="15" station="长沙市电影超市A2 厅" time="19:00"
- describtion="反映对越自卫反击战时期中国军人的故事片">
- </film>
- <film name="这里的黎明静悄悄" price="15" station="长沙市电影超市A3 厅"
- time="19:00"
- describtion="反映对越自卫反击战时期中国军人的故事片">
- </film>
- <film name="子夜" price="15" station="长沙市电影超市B1 厅" time="19:00"
- describtion="反映对越自卫反击战时期中国军人的故事片">
- </film>
- </common>
- package src;
- import org.w3c.dom.*;
- import org.xml.sax.*;
- import org.xml.sax.helpers.*;
- import javax.xml.parsers.*;
- class MyXMLReader extends DefaultHandler {
- // Fields
- private int index;
- private Locator locator;
- // Constructor
- public MyXMLReader() {
- super(); // it must be done !
- }
- // nain method
- public static void main(String[] args) {
- try {
- SAXParserFactory sf = SAXParserFactory.newInstance();
- SAXParser sp = sf.newSAXParser();
- MyXMLReader reader = new MyXMLReader();
- sp.parse(new InputSource("film.xml"), reader);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Response the startDocument event
- public void startDocument() {
- System.out
- .println("/n****************** (: 元旦电影海报 :) *****************************/n");
- }
- // Response the startElement event
- /* (non-Javadoc)
- * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
- */
- /* (non-Javadoc)
- * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
- */
- public void startElement(String uri, String localName, String qName,
- Attributes attrs) {
- System.out.println(qName);
- System.out.println(attrs);
- System.out.println(uri);
- System.out.println(localName);
- System.out.println("###############");
- if (qName.equalsIgnoreCase("film")) {
- index++;
- int attrCount = attrs.getLength();
- for (int i = 0; i < attrCount; i++) {
- String attrName = attrs.getQName(i);
- if (attrName.equalsIgnoreCase("name")) {
- System.out.println("/t 第" + index + "场,片名:<<"
- + attrs.getValue(i) + ">>");
- }
- if (attrName.equalsIgnoreCase("price")) {
- System.out.println("/t 票价:" + attrs.getValue(i));
- }
- if (attrName.equalsIgnoreCase("station")) {
- System.out.println("/t 放映地点:" + attrs.getValue(i));
- }
- if (attrName.equalsIgnoreCase("time")) {
- System.out.println("/t 放映时间:" + attrs.getValue(i));
- }
- if (attrName.equalsIgnoreCase("describtion")) {
- System.out.println("/t 影片简介:" + attrs.getValue(i));
- }
- System.out.println(this.getLocator().getLineNumber());
- System.out.println("fdfdf");
- System.out.println();
- }
- }
- }
- // Response the endDocument event
- public void endDocument() {
- System.out.println("/t/t/t/t/t/t/t------ 共有" + index + "场电影要放映");
- }
- // Response the endElement event
- public void endElement(String uri, String localName, String qName) {
- // add your codes if neccessary ...
- }
- // Print the fata error information
- public void fatalError(SAXParseException e) {
- System.out.println("/nFatal error information -->");
- System.out.println("/t" + e.getMessage());
- System.out.println("/tAt line " + locator.getLineNumber() + ",column "
- + locator.getColumnNumber());
- }
- // Print the usual error information
- public void error(SAXParseException e) {
- System.out.println("/nUsual error information -->");
- System.out.println("/t" + e.getMessage());
- System.out.println("/tAt line " + locator.getLineNumber() + ",column "
- + locator.getColumnNumber());
- }
- // Print the warning information
- public void warning(SAXParseException e) {
- System.out.println("/nWarning information -->");
- System.out.println("/t" + e.getMessage());
- System.out.println("/tAt line " + locator.getLineNumber() + ",column "
- + locator.getColumnNumber());
- }
- // Store the error locator object
- public void setDocumentLocator(Locator lct) {
- locator = lct;
- }
- public Locator getLocator() {
- return locator;
- }
- public void setLocator(Locator locator) {
- this.locator = locator;
- }
- }// End class MyXMLReader
更多推荐
MyXMLReader
发布评论