1.如下所示:控制台报错如图:
2.后台代码为:
3.xml文件如下:
求大神解决,急,在线等,百度了很多方法都不管用,求大神解决!!
用你的代码试了下,没出现错误,如果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);
}
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.
8 回答6.6k 阅读
4 回答717 阅读✓ 已解决
2 回答3.4k 阅读
3 回答1.9k 阅读✓ 已解决
1 回答2.2k 阅读✓ 已解决
1 回答2.1k 阅读✓ 已解决
1 回答974 阅读✓ 已解决
统一回答上述大神的问答,问题已经解决了,问题所在为读取文件时,不应将其设置静态变量;如图:
应改为如下: