search

Home  >  Q&A  >  body text

java - When reading a file, the console reports the following error, please solve it! ! !

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! !

習慣沉默習慣沉默2699 days ago947

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦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);
    

    }

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再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

    reply
    0
  • 某草草

    某草草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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-03 11:45:26

    Encoding problem? Try changing the xml file attributes to UTF-8

    reply
    0
  • Cancelreply