Why my DOM parser cant read UTF-8

后端 未结 3 1679
鱼传尺愫
鱼传尺愫 2020-12-06 05:59

I have problem that my DOM parser can´t load file when there are UTF-8 characters in XML file Now, i am aware that i have to give him instruction to read utf-8, but i don´t

相关标签:
3条回答
  • 2020-12-06 06:30

    Try to use Reader and provide encoding as parameter:

    InputStream inputStream = new FileInputStream(fileName);
    documentBuilder.parse(new InputSource(new InputStreamReader(inputStream, "UTF-8")));
    
    0 讨论(0)
  • 2020-12-06 06:32

    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);
    
    0 讨论(0)
  • 2020-12-06 06:45

    I used what Eugene did up there and changed it a little.

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    
    FileInputStream in = new FileInputStream(new File("XML.xml"));
    Document doc = dBuilder.parse(in, "UTF-8");
    

    though this will be read as UTF-8 if you are printing in eclipse console it won't show any 'UTF-8' characters unless the java file is saved as 'UTF-8', or at least that what happened with me

    0 讨论(0)
提交回复
热议问题