XML SAX 解析
我正在试图解析一个大概600M的XML文档.只解析其中一部分元素.
很奇怪,如果XML文档不大的时候,我的程序完全可以运行完成.
如果XML文档很大.我的程序一直没有反应.但不报错,也没有内存溢出的错误...
老师说,如果XML文档600 M,解析的时间可能会超过几十分钟,可是我苦苦等了半个小时.也没有结束解析.
请帮我分析
我的程序如下:
package com.wsy;
import java.util.*;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Xml_parser extends DefaultHandler{
String m="";
//Stack<String> tagsStack=new Stack<String>();
ArrayList<convert> author=new ArrayList<convert>(20);
ArrayList<convert> publikation=new ArrayList<convert>(20);
int markierung=0;
// authors s=null;
//calling by Begining of the file
public void startDocument() throws SAXException {
System.out.println("startDocument: ");
}
//calling by ending of the file
public void endDocument() throws SAXException {
System.out.println("enddocument: ");
}
//calling by beginging of tag
public void startElement(String uri,
String localName,
String qName,
Attributes attributes) throws SAXException{
if(qName.equals("author")||qName.equals("editor")||qName.equals("book")||qName.equals("incollection")||qName.equals("title"))
m=qName;
//else
//tagsStack.pop();
}
//calling by ending of tag
public void endElement(String namespaceURI,String localName,String qName)throws SAXException{
while(!m.equals(""))
{m="";}
}
//process of the Element
public void characters(char[] chs,int start,int length)throws SAXException{
String data=new String(chs,start,length);
String tag=m;
convert conv=new convert();
if(tag.equals("author")||tag.equalsIgnoreCase("editor"))
{conv.set_name(data);
conv.set_Markierung(markierung);
author.add(conv);}
if(tag.equals("title"))
{conv.set_name(data);
conv.set_Markierung(markierung);
publikation.add(conv);};
if(tag.equals("book")||tag.equals("incollection"))
markierung+=1;
// tagsStack.pop();
}
public void reader(String filename){
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser saxParser=spf.newSAXParser();
saxParser.parse(new File(filename),this);
//XMLReader xr=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
//xr.setContentHandler(new SaxTest());
// xr.parse(new InputSource(new FileReader("test.xml")));
}catch (Exception e)
{
e.printStackTrace();
}
}
}
main方法 通过 一个类调用 上面类的对象。
package com.wsy;
import org.xml.sax.SAXException;
public class Start {
public static void main(String[] args)throws SAXException{
Select_sql sql=new Select_sql();
System.out.println("Parser running... ");
//sql.delet_oldauthor();
// sql.delet_oldpublication();
//sql.Insert_author();
//sql.Insert_publication();
System.out.println("结束");
}
}
在强调一遍,XML文档小的时候马上就可以处结果。如果文档变大,等了30分钟还没有任何"结束"的反应
[解决办法]
你可以试下,边解析边输出吗,看有没在运行呢..就知道结果了..