【发布时间】:2014-11-25 20:38:16
【问题描述】:
我试图弄清楚如何在使用 Apache 给出的示例时简单地排除 BOM。
我正在从 Internal Storage 读取一个文件,并首先将其转换为 String。然后我把它转换成ByteArray,这样我得到一个InputStream。然后我检查 BOMInputStream 的 BOM,因为我遇到了“意外令牌”的错误。 如果我有 BOM,现在我不知道如何排除它。
代码:
StringBuffer fileContent = new StringBuffer("");
String temp = "";
int ch;
try{
FileInputStream fis = ctx.openFileInput("dataxml");
try {
while( (ch = fis.read()) != -1)
fileContent.append((char)ch);
temp = temp + Character.toString((char)ch);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
InputStream ins = new ByteArrayInputStream(temp.getBytes(StandardCharsets.UTF_8));
BOMInputStream bomIn = new BOMInputStream(ins);
if (bomIn.hasBOM()) {
// has a UTF-8 BOM
}
xpp.setInput(ins,"UTF-8");
parseXMLAndStoreIt(xpp);
ins.close();
文件名为“dataxml”,我用openFileOutput存储在不同的类中。
【问题讨论】:
标签: java android xml xml-parsing byte-order-mark