remove non-UTF-8 characters from xml with declared encoding=utf-8 - Java

后端 未结 6 1577
再見小時候
再見小時候 2020-12-13 14:42

I have to handle this scenario in Java:

I\'m getting a request in XML form from a client with declared encoding=utf-8. Unfortunately it may contain not utf-8 charact

6条回答
  •  被撕碎了的回忆
    2020-12-13 15:39

    I faced the same problem while reading files from a local directory and tried this:

    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document xmlDom = db.parse(new InputSource(in));
    

    You might have to use your network input stream instead of FileInputStream.

    -- Kapil

提交回复
热议问题