添加另一个答案,因为这个问题对于相关的 Google 搜索仍然很高 - aalto-xml 0.9.7(2011 年 3 月)具有异步 XML pasing。这允许您传递任意大小的文档块以继续解析,并使用新的 StaX 事件类型EVENT_INCOMPLETE
来指示输入缓冲区已用尽并且文档仍然不完整。
这是Tatu Salorant(作者)的示例:
byte[] msg = "<html>Very <b>simple</b> input document!</html>".getBytes();
AsyncXMLStreamReader asyncReader = new InputFactoryImpl().createAsyncXMLStreamReader();
final AsyncInputFeeder feeder = asyncReader.getInputFeeder();
int inputPtr = 0; // as we feed byte at a time
int type = 0;
do {
// May need to feed multiple "segments"
while ((type = asyncReader.next()) == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
feeder.feedInput(msg, inputPtr++, 1);
if (inputPtr >= msg.length) { // to indicate end-of-content (important for error handling)
feeder.endOfInput();
}
}
// and once we have full event, we just dump out event type (for now)
System.out.println("Got event of type: "+type);
// could also just copy event as is, using Stax, or do any other normal non-blocking handling:
// xmlStreamWriter.copyEventFromReader(asyncReader, false);
} while (type != AsyncXMLStreamReader.END_DOCUMENT);