使用DOM4J解析出現錯誤報:檔案提前結束
阿新 • • 發佈:2018-12-19
關於DOM4J解析出現錯誤報:檔案提前結束
出錯程式碼
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class ReadXML {
public static void main(String[] args) throws Exception {
String path = "F:\\myclipseworkspace\\dom4j\\src\\db.xml";
InputStream in = new FileInputStream(path) ;
OutputStream os = new FileOutputStream(path);
SAXReader reader = new SAXReader();
Document document = reader.read(path);
document.getRootElement().addElement("user").addElement("username").setText("123");
XMLWriter writer = new XMLWriter(new FileOutputStream(path),OutputFormat.createPrettyPrint ());
writer.write(document);
writer.close();
}
}
讀取的XML檔案
出錯資訊
Exception in thread "main" org.dom4j.DocumentException: Error on line 1 of document file:///F:/myclipseworkspace/dom4j/src/db.xml : 檔案提前結束。
at org.dom4j.io.SAXReader.read(SAXReader.java:466)
at org.dom4j.io.SAXReader.read(SAXReader.java:307)
at cn.aylog.dom4j.ReadXML.main(ReadXML.java:21)
Caused by: org.xml.sax.SAXParseException; systemId: file:///F:/myclipseworkspace/dom4j/src/db.xml; lineNumber: 1; columnNumber: 1; 檔案提前結束。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1014)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at org.dom4j.io.SAXReader.read(SAXReader.java:449)
... 2 more
出錯原因分析及解決方案
錯誤:建立檔案的輸入流的時候,同時建立輸出流.造成檔案還讀取的時候就已經輸出了,又怎麼能讀取呢.
解決方案:為了避免出錯必須在檔案讀取之後才能建立檔案的輸出流.
出錯後的xml檔案已經被清空了
正確的程式碼
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class ReadXML {
public static void main(String[] args) throws Exception {
String path = "F:\\myclipseworkspace\\dom4j\\src\\db.xml";
InputStream in = new FileInputStream(path);
SAXReader reader = new SAXReader();
Document document = reader.read(path);
//將輸出流定義到dom4j讀取後面
OutputStream os = new FileOutputStream(path);
document.getRootElement().addElement("user").addElement("username").setText("123");
XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
}
}
總結
在讀取檔案時過於著急,為了程式碼風格好看總想一次性定義完變數,看來這是一個不好的習慣.下次注意,用到什麼變數再定義,避免出現同樣的錯誤.