1. As shown below: The console error message is as follows:
2. The background code is:
3.xml file is as follows:
Please ask God to solve it. Urgent, wait online. Many methods on Baidu don’t work. Please ask God to solve it! !
伊谢尔伦2017-07-03 11:45:26
I tried it with your code and no errors occurred. If there is an error with dom4j, try something else
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);
}
过去多啦不再A梦2017-07-03 11:45:26
I think it’s a problem with your xml file. The error translated is: The file ended early! The xml file may have no end tag, wrong start tag, etc. You may encounter this error! Please post your complete xml file
某草草2017-07-03 11:45:26
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.