Dav*_*bji 4 java xml android byte-order-mark xml-parsing
我试图弄清楚如何在使用 Apache 给出的示例时简单地排除 BOM。我正在从内部存储读取文件并首先将其转换为String. 然后我将其转换为ByteArray这样我就得到了一个InputStream. 然后我检查BOMInputStreamBOM,因为我遇到了“意外令牌”错误。现在我不知道如何排除 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();
Run Code Online (Sandbox Code Playgroud)
文件名是“dataxml”,我将其存储在不同的类中openFileOutput。
use*_*552 10
您可以将初始流包装在 BOMInputStream 中:
InputStream stream = new BOMInputStream(inputStream);
// code using stream goes here
Run Code Online (Sandbox Code Playgroud)
这种方式stream会自动跳过 BOM 前缀。BOMInputStream存在于 Apache Commons IO 库中。
| 归档时间: |
|
| 查看次数: |
13034 次 |
| 最近记录: |