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試試呢
相關文章:
