簡體   English   中英

Java - 將xml文件從ISO-8859-2轉換為UTF-8

[英]Java - transform xml file from ISO-8859-2 to UTF-8

我需要將文件從ISO-8859-2 charset轉換為UTF-8。

我的代碼是:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(file);


        DOMSource domSource = new DOMSource(doc);

        String fileName2 = UUID.randomUUID().toString() + "222";
        Writer out = new OutputStreamWriter(new FileOutputStream("/Users/user/Kohana/" + fileName2 + ".xml"), "UTF8");

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(domSource, new StreamResult(out));

但問題是,在轉換后,文件仍然是ISO-8859-2。

我究竟做錯了什么 ?

  1. 使用“ISO-8859-2”Reader / Stream讀取xml
  2. 使用xslt變換添加<?xml version=“1.0” encoding=“utf-8”?>標題
  3. 使用用“UTF-8”編碼構造的Writer / Stream

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM