我从远程获取一个包含XML字符串的InputStream,但是很不幸,这个XML文档里面有一种非法字符串影响我的正常使用,这个字符串的16进制代码为“0x1a”(10进制应该为整数27)。
如果我直接将这个InputStream转换为字符串,肯定不能使用,于是我想在转化的过程中将这些非法字符串剔除,我的思路如下:
InputStream in = (InputStream) conn.getInputStream();
List byteList = new ArrayList();
int byteInt = 0;
while ((byteInt = in.read()) != -1) {
    String hexCode = Integer.toHexString(byteInt);
    if (!hexCode.equalsIgnoreCase("0x1a"))
        byteList.add(new Byte((byte)byteInt));
}
byte[] bytes = new byte[byteList.size()];
for (int i = 0; i < bytes.length; i++) {
    bytes[i] = ((Byte)byteList.get(i)).byteValue();
}
in.close();
String xmlString = new String(bytes);
但是这样做完以后,程序里面仍然发现字符串xmlString里面有非法字符串“0x1a”,我比较郁闷。