dom4j建立xml檔案utf8亂碼

2021-09-02 12:44:27 字數 474 閱讀 9163

最近在專案中要求做xml的轉換,要求生成編碼是utf-8的是,在生成時

document document = documenthelper.createdocument();

document.setxmlencoding("utf-8");

這個雖然設定了編碼,但是生成完後使用nodepad++開啟還是ansi格式開啟的,如果使用utf-8就會顯示亂碼,在linux上更明顯,要想讓生成的檔案為utf-8格式的需要使用下面的方式

xmlwriter xmlwriter = new xmlwriter(new fileoutputstream(file),「utf-8」);

document.setxmlencoding("utf-8");

xmlwriter.write(document);

xmlwriter.flush();

xmlwriter.close();

在寫入流檔案時設定編碼即可。

DOM4J建立xml檔案

dom4j建立xml主要會用到document和element 其中 addelement 新增子節點 addattribute 新增屬性 示例 public static void main string args throws exception 建立文件的根節點 document docume...

Dom4j 解析 建立xml

dom4j 解析xml 最近在進行介面的呼叫,接收過來是xml格式,返回格式也需要是xml格式。所以在這裡進行記錄一下!public class tests 輸出格式 msgcode name age aa cardrow list msgbody msgport saxreader reader ...

dom4j 使用dom4j生成xml

使用org.dom4j.element 建立xml 生成service.xml檔案 param tran 交易物件 param filepath 資料夾路徑 public static void exportservicexml listtranlist,string filepath servic...