我想解析一個xml文檔,搜索後發現sax是最好的選擇,但文檔非常大(1.5 GB)等待7分鐘,但它仍在運行 我的問題是,這是正常的嗎?或者我可以做得更好?使用Sax解析大型XML文件
public static void main(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
int c = 0;
boolean id = false;
boolean value = false;
boolean orgin = false;
boolean note = false;
@Override
public void startElement(String uri, String localName, String eName,
Attributes attributes) throws SAXException {
if (eName.equalsIgnoreCase("ID")) {
id = true;
}
if (eName.equalsIgnoreCase("VALUE")) {
value = true;
}
if (eName.equalsIgnoreCase("ORGIN")) {
orgin = true;
}
if (eName.equalsIgnoreCase("NOTE")) {
note = true;
}
}
@Override
public void endElement(String uri, String localName,
String eName) throws SAXException {
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (id) {
System.out.println(new String(ch, start, length));
id = false;
System.out.println("record num : "+c++);
}
if (value) {
System.out.println(new String(ch, start, length));
value = false;
}
if (orgin) {
System.out.println(new String(ch, start, length));
orgin = false;
}
if (note) {
System.out.println(new String(ch, start, length));
note = false;
}
}
};
saxParser.parse("./transactions.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}
你是什麼意思與1.5? 1.5 mb? – Christian
@Christian 1.5 GB –
看看這個:http://stackoverflow.com/questions/3411773/parsing-big-xml-files-using-sax-parser-skip-some-lines-tags –