xml动态读取分段读取

package com.audaque.module;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.io.SAXReader;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class MyXMLReader2SAX extends DefaultHandler implements EntityResolver {

    @Override
    public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
        return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
    }

    java.util.Stack tags = new java.util.Stack();

    public MyXMLReader2SAX() {
        super();
    }

    public static void main(String args[]) {
        long lasting = System.currentTimeMillis();
        try {
            SAXParserFactory sf = SAXParserFactory.newInstance();
            sf.setValidating(false);
            SAXParser sp = sf.newSAXParser();

            SAXReader reader1 = new SAXReader();
            reader1.setEntityResolver(new MyXMLReader2SAX()); // ignore dtd

            MyXMLReader2SAX reader = new MyXMLReader2SAX();
            sp.parse(new InputSource("F:\\BaiduNetdiskDownload\\dblp.xml\\dblp.xml"), reader);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
                + "毫秒");
    }

    public void characters(char ch[], int start, int length)
            throws SAXException {
        String tag = (String) tags.peek();
        if (tag.equals("article")) {
            System.out.print("article:" + new String(ch, start, length));
        }
       else if (tag.equals("mdate")) {
            System.out.print("mdate:" + new String(ch, start, length));
        }
       else if (tag.equals("key")) {
            System.out.print("key:" + new String(ch, start, length));
        }
       else if (tag.equals("author")) {
            System.out.print("作者:" + new String(ch, start, length));
        }
       else if (tag.equals("title")) {
            System.out.println("标题:" + new String(ch, start, length));
        }
       else if (tag.equals("pages")) {
            System.out.println("页码:" + new String(ch, start, length));
        }
       else if (tag.equals("year")) {
            System.out.println("年份:" + new String(ch, start, length));
        }
       else if (tag.equals("volume")) {
            System.out.println("volume:" + new String(ch, start, length));
        }
       else if (tag.equals("number")) {
            System.out.println("number:" + new String(ch, start, length));
        }
       else if (tag.equals("url")) {
            System.out.println("url:" + new String(ch, start, length));
        }
       else if (tag.equals("ee")) {
            System.out.println("ee:" + new String(ch, start, length));
        }
       /* if (tag.equals("journal")) {
            System.out.println("标题:" + new String(ch, start, length));
        }*/

    }

    public void startElement(String uri, String localName, String qName,
                             Attributes attrs) {
        tags.push(qName);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值