java - 读文件时控制台报如下错误,求解决!!!
问题描述
1.如下所示:控制台报错如图:
2.后台代码为:
3.xml文件如下:
求大神解决,急,在线等,百度了很多方法都不管用,求大神解决!!
问题解答
回答1:用你的代码试了下,没出现错误,如果dom4j出错,试试别的
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();ClassPathResource resource = new ClassPathResource('reportType.xml');Document doc = builder.parse(resource.getInputStream());Element root = doc.getDocumentElement();NodeList list = root.getChildNodes();for (int i = 0, len = list.getLength(); i < len; i++) {
Node node = list.item(i);
}
回答2:我觉得是你xml文件的问题,错误翻译过来是:文件提前结束!xml文件可能没有结尾标签、开头标签错误等都可能遇到这种错误!你贴下你完整的xml文件
回答3:Problem: Input stream is opened and read bytes from it, passed the same to document builder to parse method. so it caused the exception saying premature end of file.Solution: Pass fresh input stream which is opened and not read anything (bytes) before passing to parse method of DocumentBuilder object.回答4:
编码问题?xml文件属性改为UTF-8试试呢
相关文章: