我不得不投解決:我收到一個致命的錯誤使用XOM:「[致命錯誤]:1:1:文件過早結束。」
xmlSb.append(is.read());
爲
xmlSb.append((char) is.read());
來解決這個問題。但是,我正在使用不同的方法來下載文件,在我的項目的這一部分中不再有任何問題。
---舊帖子---
我似乎沒有被做重複的帖子,所以這裏的細節...
當我使用非靜態方法解析文檔Builder.build()從XOM(XML對象模型,一個Java庫),在Eclipse控制檯我得到:
[Fatal Error] :1:1: Premature end of file.
這是一個嚴重的問題,即強行中止程序的運行。
我使用的方法的內容如下......
public boolean buildDocument() {
boolean out = true;
Builder b = null;
b = new Builder();
String xml = "";
String newXml = "";
StringBuilder xmlSb = new StringBuilder();
URL xmlUrl = null;
try {
xmlUrl = new URL(this.packAddr);
} catch (MalformedURLException e1) {
Pcdl.log.severe(e1.getMessage());
out = false;
}
InputStream is;
try {
is = xmlUrl.openStream();
while (is.available() > 0) {
xmlSb.append(is.read());
}
} catch (IOException e1) {
e1.printStackTrace();
Pcdl.log.severe(e1.getMessage());
out = false;
}
xml = xmlSb.toString();
// Add more lines if needed. I hope that nobody actually had to use any XML entities.
newXml = Util.removeUTF8BOM(xml)
.replace("&", "&")
;
Pcdl.log.info("XML Data Size: " + newXml.length() + " B.");
System.out.println(newXml);
// Actually build it.
try {
this.doc = b.build(new ByteArrayInputStream(newXml.getBytes("UTF-8")));
} catch (ParsingException | IOException e) {
Pcdl.log.severe("Document build error: " + e.getMessage());
System.out.println(e.getMessage());
out = false;
}
System.out.println(this.doc);
return out;
}
我有整個程序在GitHub的庫:「http://github.com/Treyzania/PraseocraftDL/」。如果您希望以任何方式爲這個項目做出貢獻,請做!只需製作一個叉子,我就會看到它裏面有什麼。
此方法來自「com.treyzania.praseocraft.ftb.downloader.PackFile」。 我知道我的軟件包名稱很長,但現在改變它們爲時已晚。我正在開發的這個項目是一個採用XML文檔(例如:http://pastebin.com/raw.php?i=B42in4c5)的程序,並將其編譯到%appdata%/。mc-pcdl/packs /(或類似文件)中的Minecraft modpack中,並編輯其他幾個文件正確地向Minecraft啓動器註冊數據。我試圖保持這個項目儘可能抽象,所以它可能看起來像有更多的類比必要的。我也嘗試儘可能多地去解決這個問題。沒有任何工作。