为什么我的 DOM 解析器无法读取 UTF-8

2022-09-03 17:25:20

我有问题,我的DOM解析器无法加载文件,当XML文件中有UTF-8字符时,现在,我知道我必须给他指令来阅读utf-8,但我不知道如何把它放在我的代码中, 它是:

File xmlFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

我知道有方法setencoding(),但我不知道把它放在我的代码中的位置...


答案 1

试试这个。为我工作

        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);

答案 2

尝试使用 Reader 并提供编码作为参数:

InputStream inputStream = new FileInputStream(fileName);
documentBuilder.parse(new InputSource(new InputStreamReader(inputStream, "UTF-8")));