返回顶部

收藏

小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

发表评论