本文介绍了为什么我的DOM解析器不能读取UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题,我的DOM解析器无法在XML文件中有UTF-8字符时加载文件现在,我知道我必须给他指令来读取utf-8,但是我不知道如何把它放在我的代码
中,它是:
文件xmlFile = new File(fileName) ;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
文档doc = dBuilder.parse(xmlFile);
doc.getDocumentElement()。normalize();
我知道有方法setencoding(),但我不知道在哪里放在我的代码中...
解决方案
为我工作
InputStream inputStream = new FileInputStream(completeFileName);
读者阅读器=新的InputStreamReader(inputStream,UTF-8);
InputSource is = new InputSource(reader);
is.setEncoding(UTF-8);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
文档doc = dBuilder.parse(is);
I have problem that my DOM parser can´t load file when there are UTF-8 characters in XML fileNow, i am aware that i have to give him instruction to read utf-8, but i don´t know how to put it in my codehere it is:
File xmlFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
i am aware that there is method setencoding(), but i don´t know where to put it in my code...
解决方案
Try this. Worked for me
InputStream inputStream= new FileInputStream(completeFileName);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
这篇关于为什么我的DOM解析器不能读取UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!