java 解析xml是使用dom4j空行以及中文亂碼的處理

dom4j 在刪除節點後或者 創建xml文本時,會保留一些空行。可以設置OutputFormat來進行設置。

第二行空行的解決方法

            OutputFormat format = OutputFormat.createPrettyPrint();  
            format.setEncoding("UTF-8");  
            format.setNewLineAfterDeclaration(false);  

刪除節點後的空行處理

        OutputFormat format = new OutputFormat();
        format.setTrimText(true);
        format.setIndent(true);
        format.setNewlines(true);
        format.setNewLineAfterDeclaration(false);

中文亂碼

 OutputFormat format = new OutputFormat();
 format.setEncoding("UTF-8"); 

最後在創建XmlWriter傳入Format參數

XMLWriter   xmlWriter = new XMLWriter(out, format);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章