我正在尝试实现一个从套接字读取并返回完整XML文档的XMLReader:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Stack;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
public class MyXmlReader {
private final XMLInputFactory infactory = XMLInputFactory.newInstance();
private final InputStream inStream;
public MyXmlReader(final InputStream inStream){
this.inStream= inStream;
}
private String readUntilEndOfXml() throws XMLStreamException{
final XMLEventReader eventReader= infactory.createXMLEventReader(inStream);
final StringBuilder sb= new StringBuilder();
final Stack<String> openTags= new Stack<String>();
boolean endOfXml= false;
while (eventReader.hasNext() && !endOfXml){
final XMLEvent nextEvent= eventReader.nextEvent();
sb.append(nextEvent.toString());
if (nextEvent.isStartElement()){
openTags.add(nextEvent.toString());
}else if (nextEvent.isEndElement()){
openTags.pop();
if (openTags.isEmpty()){
endOfXml= true;
}
}
}
eventReader.close();
return sb.toString();
}
public static void main(String[] args) throws Exception {
final File f= new File("/tmp/double.xml");
final MyXmlReader r= new MyXmlReader(new FileInputStream(f));
String xml;
while((xml= r.readUntilEndOfXml()) != null){
System.out.println("Got an XML");
}
}
}
但是,如果到达文件的末尾(或从套接字读取时套接字已关闭),我会收到以下异常:
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:594)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:85)
at com.siemens.bfr2.plato.MyXmlReader.readUntilEndOfXml(MyXmlReader.java:40)
at com.siemens.bfr2.plato.MyXmlReader.main(MyXmlReader.java:125)
我期待检查eventReader.hasNext()
就足够了。为什么eventReader.hasNext()
仍在返回true
,实际上没有更多数据?