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試試呢
相關文章:
1.排行榜angular.js - angular TypeError: Cannot read property ’id’ of undefined? 1. javascript - 求助:vue2.0中使用element ui遇到的問題 2. javascript - weexpack編譯出錯 Cannot find module ’escape-string-regexp’ 3. mysql - sysbench cpu測試的結果看不懂,求解 4. javascript - 在一個a標簽內添加了一個單擊事件,想在時間里面給這個a標簽添加類樣式 5. angular.js - angularJs里面的這種判斷自動生成的注釋可以去掉嗎? 6. html5 - 上下居中對齊實際上無法實現吧? 7. javascript - 關于數組的循環遍歷問題 8. angular.js - 使用angularjs的路由調用界面,從controller上可以調用,但是頁面上獲取不到數據。 9. 在MySQL中,執行了DELIMITER // 這個語句后分號還有什么作用? 10. javascript - canvas toDataURL()跨域問題