返回顶部

收藏

小GUI程序,使用SAX解析XML并显示

更多

XmlView1.java

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import org.xml.sax.*;
import com.sun.xml.parser.*;
import org.xml.sax.helpers.*;

/**
 * XmlView1 - a small AWT GUI program that illustrates use
 * of the javasoft SAX implementation.  Note that this
 * little program cannot parse any XML file that specifies
 * a DTD, because SAX is very fussy about DTD URIs.
 * XmlView2 is more sophisticated, and can handle XML files
 * that have DTDs.
 */

public class XmlView1 extends Frame implements ActionListener {
    List contents;
    Label caption;
    TextField filename;
    Button goButton;

    /**
     * Constructor - create the AWT GUI
     */
    public XmlView1() {
   super("XmlView1");
   contents = new List();
   caption = new Label("XML file:");
   filename = new TextField();
   goButton = new Button("PARSE");
   Panel topPanel = new Panel();
   topPanel.setLayout(new BorderLayout(2,2));
   topPanel.add("West",caption);
   topPanel.add("Center",filename);
   topPanel.add("East", goButton);
   add("North", topPanel);
   add("Center", contents);
   setSize(400,500);
   goButton.addActionListener(this);
    }

    /**
     * This class is a SAX listener that accepts the parse
     * events during parsing.
     */
    public class ListingHandler extends HandlerBase {
   int depth = 0;
   int count = 0;

   private String indent(String t) {
       StringBuffer s = new StringBuffer(depth + t.length());
       for(int i = 0; i < depth; i++) s.append("    ");
       s.append(t);
       return s.toString();
   }
   public void characters(char [] ch, int start, int length) {
       String s = indent("CHARS" + length + ": ");
       s += new String(ch, start, length);
       contents.add(s);
   }

   public void endDocument() {
       depth -= 1;
       String s = indent("EndDocument");
       contents.add(s);
   }

   public void endElement(String name) {
       depth -= 1;
       String s = indent("EndElement: ");
       s += name;
       contents.add(s);
   }

   public void warning(SAXParseException e) {
       depth -= 1;
       String s = indent("Warning! ");
       s += e.toString();
       contents.add(s);
   }
   public void error(SAXParseException e) {
       depth -= 1;
       String s = indent("ERROR! ");
       s += e.toString();
       contents.add(s);
   }
   public void fatalError(SAXParseException e) {
       depth = 0;
       String s = indent("FATAL ERROR!!! ");
       s += e.toString();
       contents.add(s);
   }
   public void processingInstruction(String target, String data) {
       String s = indent("ProcInstr: ");
       s += target;
       s += "/";
       s += data;
       contents.add(s);
   }
   public void startDocument() {
       depth = 0;
       String s = "StartDocument";
       contents.add(s);
       depth += 1;
       count += 1;
   }
   public void startElement(String name, AttributeList alist) {
       String s = indent("StartElement: ");
       s += name;
       contents.add(s);
       depth += 3;
       doAttributeList(alist);
       depth -= 3;
       depth += 1;
       count += 1;
   }
   private void doAttributeList(AttributeList a) {
       int i;
       String s;
       for(i = 0; i < a.getLength(); i++) {
      s = indent("Attribute: ");
      s += a.getName(i);
      s += "=";
      s += a.getValue(i);
      contents.add(s);
       }
   }

    }

    /**
     * Start the parsing when the user tells us to do so.
     */
    public void actionPerformed(ActionEvent e) {
   String fn = filename.getText();
   FileInputStream fis;
   try {
       fis = new FileInputStream(fn);
   }
   catch (IOException ex2) {
       System.out.println("Bad file: " + fn);
       return;
   }

   System.out.println("Creating InputSource...");
   InputSource is = new InputSource(fis);

   System.out.println("Creating and configuring Parser...");
   com.sun.xml.parser.Parser myparser = new com.sun.xml.parser.Parser();
   myparser.setFastStandalone(true);
   org.xml.sax.Parser p = myparser;
   ListingHandler lh = new ListingHandler();
   p.setDocumentHandler(lh);
   p.setErrorHandler(lh);

   System.out.println("Clearing list..");
   contents.removeAll();

   try {
       System.out.println("Parsing..");
       p.parse(is);
       System.out.println("Done, " + lh.count + " elements parsed.");
   }
   catch (Exception se) {
       System.out.println("Parsing exception: " + se);
       contents.add("Parsing Exception Thrown!!!");
   }
   try { fis.close(); } catch (IOException e4) { }
    }

    /**
     * Main method - kick off the program.
     */
    public static void main(String args[]) {
   XmlView1 v1 = new XmlView1();
   v1.show();
   v1.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
      System.out.println("All done.  Bye.");
      System.exit(0);
       }
   } );
    }
}

标签:jQuery,XML,SAX

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Hiro 发表 2009-09-21 03:33:02 XPath:语法及示例,前端学习收集
  2. 歪脖骇客 发表 2016-05-05 03:53:37 使用jQuery解析XML
  3. Hiro 发表 2009-07-10 03:52:23 jQuery:书籍展示效果
  4. Shawphy 发表 2011-09-04 11:57:07 Lorem ipsum 是什么?顺便推荐一款假文生成器MoreText
  5. 阿安 发表 2013-02-04 05:03:09 jQuery1.9中被删除的API
  6. 王子墨 发表 2011-04-01 17:47:04 随手写的、代替 marquee的一段jQuery代码
  7. 城主 发表 2013-01-22 03:41:43 wordpress 后台页面模板动态切换
  8. Tamim Shahriar (nore 发表 2012-09-26 03:25:21 Simple and easy XML parsing with lxml
  9. xmouse 发表 2013-03-25 07:04:32 jQuery视频教程6:使用选择器获得要操作的元素
  10. licess 发表 2013-04-06 02:34:13 使用又拍云(upyun)JS库CDN加速wordpress jquery
  11. 520dujiangkuan 发表 2013-04-27 00:33:34 jquery tab切换
  12. lixiphp 发表 2013-05-07 12:49:55 工作笔记

发表评论