频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

从任意数据结构生成XML解析器产生SAX事件

来源: 作者: 出处:巧巧读书 2006-08-09 进入讨论组
下一页 1 2 

    在j2ee1.4标准教材里看到一个很有趣的例子,从任意数据结构生成XML解析器产生SAX事件.数据结构可以是文本文件,PDF格式文档等.关键是自己解析这些数据源.另外一个有意思的地方是观察者模式的应用.所以就粗糙的改了一下并完整到可以测试运行.观察者模式简略UML图:


从任意数据结构生成XML解析器产生SAX事件


具体实现 被观察者对象ParseXMLSubject类:
package test;

import java.io.*;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.*;

public class ParseXMLSubject implements XMLReader {
    ContentHandler handler;

    String nsu = "";
    Attributes atts = new AttributesImpl();
    String rootElement = "addressbook";
    String indent = "\n    ";

    public ParseXMLSubject(){

    }

    public ContentHandler getContentHandler() {
        return handler;
    }

    public void parse(InputSource input) throws IOException, SAXException {
        try {
            // Get an efficient reader for the file
            java.io.Reader r = input.getCharacterStream();
            BufferedReader br = new BufferedReader(r);

            // Read the file and display it's contents.
            String line = br.readLine();

            while (null != (line = br.readLine())) {
                if (line.startsWith("email:")) {
                    break;
                }
            }

            if (handler == null) {
                throw new SAXException("No content handler");
            }

            // Note:
            // We're ignoring setDocumentLocator(), as well
            handler.startDocument();
            handler.startElement(nsu, rootElement, rootElement, atts);

            output("email",  line);
            line = br.readLine();
            output("html", line);
            line = br.readLine();
            output("firstname",  line);
            line = br.readLine();
            output("lastname", line);
            line = br.readLine();
            output("work",  line);
            line = br.readLine();
            output("home", line);
            line = br.readLine();
            output("fax",  line);
            line = br.readLine();
            output("pager", line);
            line = br.readLine();
            output("cell",  line);
            handler.ignorableWhitespace("\n".toCharArray(), 0, // start index
                                        1 // length
                    );
            handler.endElement(nsu, rootElement, rootElement);
            handler.endDocument();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void parse(String systemId) throws IOException, SAXException {
    }


    public DTDHandler getDTDHandler() {
        return null;
    }


    public EntityResolver getEntityResolver() {
        return null;
    }

查看 http://www.qqread.com/j2ee/y196690.html 更多文章 更多内容请看数据结构XML详解基础教程专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章