小GUI程序,使用SAX解析XML并显示
- 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);
- }
- } );
- }
- }
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论