免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4433 | 回复: 0

小GUI程序,使用SAX解析XML并显示 [复制链接]

论坛徽章:
0
发表于 2011-01-20 02:05 |显示全部楼层
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import org.xml.sax.*;
  5. import com.sun.xml.parser.*;
  6. import org.xml.sax.helpers.*;

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

  15. public class XmlView1 extends Frame implements ActionListener {
  16.     List contents;
  17.     Label caption;
  18.     TextField filename;
  19.     Button goButton;

  20.     /**
  21.      * Constructor - create the AWT GUI
  22.      */
  23.     public XmlView1() {
  24.    super("XmlView1");
  25.    contents = new List();
  26.    caption = new Label("XML file:");
  27.    filename = new TextField();
  28.    goButton = new Button("PARSE");
  29.    Panel topPanel = new Panel();
  30.    topPanel.setLayout(new BorderLayout(2,2));
  31.    topPanel.add("West",caption);
  32.    topPanel.add("Center",filename);
  33.    topPanel.add("East", goButton);
  34.    add("North", topPanel);
  35.    add("Center", contents);
  36.    setSize(400,500);
  37.    goButton.addActionListener(this);
  38.     }

  39.     /**
  40.      * This class is a SAX listener that accepts the parse
  41.      * events during parsing.
  42.      */
  43.     public class ListingHandler extends HandlerBase {
  44.    int depth = 0;
  45.    int count = 0;

  46.    private String indent(String t) {
  47.        StringBuffer s = new StringBuffer(depth + t.length());
  48.        for(int i = 0; i < depth; i++) s.append("    ");
  49.        s.append(t);
  50.        return s.toString();
  51.    }
  52.    public void characters(char [] ch, int start, int length) {
  53.        String s = indent("CHARS" + length + ": ");
  54.        s += new String(ch, start, length);
  55.        contents.add(s);
  56.    }
  57.    
  58.    public void endDocument() {
  59.        depth -= 1;
  60.        String s = indent("EndDocument");
  61.        contents.add(s);
  62.    }

  63.    public void endElement(String name) {
  64.        depth -= 1;
  65.        String s = indent("EndElement: ");
  66.        s += name;
  67.        contents.add(s);
  68.    }

  69.    public void warning(SAXParseException e) {
  70.        depth -= 1;
  71.        String s = indent("Warning! ");
  72.        s += e.toString();
  73.        contents.add(s);
  74.    }
  75.    public void error(SAXParseException e) {
  76.        depth -= 1;
  77.        String s = indent("ERROR! ");
  78.        s += e.toString();
  79.        contents.add(s);
  80.    }
  81.    public void fatalError(SAXParseException e) {
  82.        depth = 0;
  83.        String s = indent("FATAL ERROR!!! ");
  84.        s += e.toString();
  85.        contents.add(s);
  86.    }
  87.    public void processingInstruction(String target, String data) {
  88.        String s = indent("ProcInstr: ");
  89.        s += target;
  90.        s += "/";
  91.        s += data;
  92.        contents.add(s);
  93.    }
  94.    public void startDocument() {
  95.        depth = 0;
  96.        String s = "StartDocument";
  97.        contents.add(s);
  98.        depth += 1;
  99.        count += 1;
  100.    }
  101.    public void startElement(String name, AttributeList alist) {
  102.        String s = indent("StartElement: ");
  103.        s += name;
  104.        contents.add(s);
  105.        depth += 3;
  106.        doAttributeList(alist);
  107.        depth -= 3;
  108.        depth += 1;
  109.        count += 1;
  110.    }
  111.    private void doAttributeList(AttributeList a) {
  112.        int i;
  113.        String s;
  114.        for(i = 0; i < a.getLength(); i++) {
  115.       s = indent("Attribute: ");
  116.       s += a.getName(i);
  117.       s += "=";
  118.       s += a.getValue(i);
  119.       contents.add(s);
  120.        }
  121.    }
  122.    
  123.     }

  124.     /**
  125.      * Start the parsing when the user tells us to do so.
  126.      */
  127.     public void actionPerformed(ActionEvent e) {
  128.    String fn = filename.getText();
  129.    FileInputStream fis;
  130.    try {
  131.        fis = new FileInputStream(fn);
  132.    }
  133.    catch (IOException ex2) {
  134.        System.out.println("Bad file: " + fn);
  135.        return;
  136.    }
  137.    
  138.    System.out.println("Creating InputSource...");
  139.    InputSource is = new InputSource(fis);
  140.    
  141.    System.out.println("Creating and configuring Parser...");
  142.    com.sun.xml.parser.Parser myparser = new com.sun.xml.parser.Parser();
  143.    myparser.setFastStandalone(true);
  144.    org.xml.sax.Parser p = myparser;
  145.    ListingHandler lh = new ListingHandler();
  146.    p.setDocumentHandler(lh);
  147.    p.setErrorHandler(lh);
  148.    
  149.    System.out.println("Clearing list..");
  150.    contents.removeAll();
  151.    
  152.    try {
  153.        System.out.println("Parsing..");
  154.        p.parse(is);
  155.        System.out.println("Done, " + lh.count + " elements parsed.");
  156.    }
  157.    catch (Exception se) {
  158.        System.out.println("Parsing exception: " + se);
  159.        contents.add("Parsing Exception Thrown!!!");
  160.    }
  161.    try { fis.close(); } catch (IOException e4) { }
  162.     }

  163.     /**
  164.      * Main method - kick off the program.
  165.      */
  166.     public static void main(String args[]) {
  167.    XmlView1 v1 = new XmlView1();
  168.    v1.show();
  169.    v1.addWindowListener(new WindowAdapter() {
  170.        public void windowClosing(WindowEvent e) {
  171.       System.out.println("All done.  Bye.");
  172.       System.exit(0);
  173.        }
  174.    } );
  175.     }
  176. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP