使用DOM4J解析出现错误报:文件提前结束

关于DOM4J解析出现错误报:文件提前结束

    • 出错代码
    • 读取的XML文件
    • 出错信息
    • 出错原因分析及解决方案
    • 出错后的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);
		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文件

使用DOM4J解析出现错误报:文件提前结束_第1张图片

出错信息

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文件已经被清空了

使用DOM4J解析出现错误报:文件提前结束_第2张图片

正确的代码

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();
	}
}

总结

在读取文件时过于着急,为了代码风格好看总想一次性定义完变量,看来这是一个不好的习惯.下次注意,用到什么变量再定义,避免出现同样的错误.

你可能感兴趣的:(java)