歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 使用dom讀取XML文件及對中文字符的支持

Java 使用dom讀取XML文件及對中文字符的支持

日期:2017/3/1 10:11:37   编辑:Linux編程

我本機的開發環境編碼是UTF-8;

以下這個方法正常讀取不含中文的XML文件是沒問題的

public static Element returnRootElement(String fileName) {
String deviceInformation = "";
Document document = null;
Element root = null;
try {
deviceInformation = FileResource.readFile(fileName,UiUtilPlugin.PLUGIN_ID);
DocumentBuilder builder;
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = new ByteArrayInputStream(
deviceInformation.getBytes());
document = builder.parse(is);
root = document.getDocumentElement();
return root;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

但是遇到含有中文的字符呢 就會出現這個錯誤

org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence

這是因為SAX’s parser沒有設置正確的編碼格式,導致讀取文件出錯。

那原因知道了

我下面貼一下我修改的代碼:

很簡單,加入這幾句就可以了

InputStream iStream= new ByteArrayInputStream(xmlInformation.getBytes());
Reader reader = new InputStreamReader(iStream,"GB2312");
InputSource iSource = new InputSource(reader);
iSource.setEncoding("GB2312");
document = builder.parse(iSource);

共同學習。

Copyright © Linux教程網 All Rights Reserved