如何使用Java解析大型XML文件? [英] How do I parse a large XML file using Java?
本文介绍了如何使用Java解析大型XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Java解析XML文件.
I am trying to parse an XML file using Java.
XML文件大小仅为256 kb.我正在使用DOM解析器来解析XML文件.如何解析大型XML文件内容?
The XML file size is 256 kb only. I am using a DOM parser to parse the XML file. How can I parse the large XML file content?
以下是解析文件内容的方法:
Here's the method that parses the file content:
public Document parse_a_string(StringBuffer decodedFile) {
Document doc1 = null;
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
// problem here
inStream.setCharacterStream(new StringReader(decodedFile.toString()));
doc1 = db.parse(inStream);
} catch (Exception e) {
}
return doc1;
}
文件内容位于 StringBuffer
参考对象 decodedFile
中,但是当我将其设置为 StringReader
时,它仅接受字符串.
The file content is in the StringBuffer
reference object, decodedFile
, but when I set it to StringReader
it accept only string.
推荐答案
For large documents (though I wouldn't call your's large) I'd use StAX.
这篇关于如何使用Java解析大型XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文