我正在尝试解析现有的 xhtml 文件以将额外的正文内容添加到该文件中。我正在使用以下代码:
首先,我从 Jsoup 读取正文,然后尝试将其放入 XhtmlFile
Document doc = Jsoup.parse(readFile, "UTF-8");
Elements content = doc.getElementsByTag("body");
try {
Document document=null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Create the builder and parse the file
document = (Document)factory.newDocumentBuilder().parse(finalFile);
//document.getElementsByTagName("body")append(content.toString());
//document=parserXML(finalFile);
document.getElementsByTag("body").append(content.toString());
} catch (SAXException e) {
System.out.println("SAXException>>>>>>");
e.printStackTrace();
} catch (ParserConfigurationException e) {
System.out.println("in parser configuration Exception block>>>>>>");
e.printStackTrace();
}
但我收到以下异常:
[Fatal Error] ResultParsedFile.html:1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.converter.typeconverter.EmailTypeConverter.readHTML(EmailTypeConverter.java:101)
at com.converter.typeconverter.EmailTypeConverter.callTika(EmailTypeConverter.java:64)
at com.converter.master.ApplicationMain.main(ApplicationMain.java:64)
请帮我解决这个问题...
提前致谢...
最佳答案
如果您在文件的第一个位置(1:1 表示)出现此错误,则表示该文件为空。
也许您在源代码关闭之前就开始阅读文件了?
如果您使用输入流(这里不是这种情况),当您重新使用已经用于到达文件末尾的流时,可能会发生这种情况。您需要从输入文件创建一个新的流,以便从文件的开头重置它。
关于java - 获取文件过早结束异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416477/