我试图解析现有的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)
普莱舍帮助我解决这个问题...提前
谢谢...
听起来像finalFile可能不是一个有效的XML文件... –
如果它是XHTML,你应该能够用一个普通的XML解析器解析它。 Jsoup并不是特别针对破解!X!HTML。 – bmargulies
@Jon表示我可能不会使用DOM或SAX解析器来解析,但是从jsoup我得到我需要的html文件的正文内容,我只是想将该消息注入到xhtml(finalfile)的正文中。你的时间。在这方面的任何指针.. – ramesh