国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

dom4j 改變XML聲明和編碼格式

2019-11-14 23:34:01
字體:
來源:轉載
供稿:網友
dom4j 改變xml聲明和編碼格式dom4j 改變XML編碼
Element rootElement = document.addElement("data");document.setXMLEncoding("GBK");//默認utf-8...

使用document.setXMLEncoding這樣設置而生成的xml文件仍然是utf-8編碼。

需要使用OutputFormat設置輸出文件編碼格式。

 public static void writeXMLFile(Document document,File file,String Encoding){   try {          OutputFormat format = OutputFormat.createPRettyPrint();//美化輸出 不想美化可以使用new OutputFormat();          format.setEncoding(Encoding.toUpperCase());          OutputStream out = new FileOutputStream(file);          XMLWriter writer = new XMLWriter(out,format);          writer.write(document);          writer.close();        }catch (IOException e) {          e.printStackTrace();        }

使用OutputFormat,可以設置xml輸出文件編碼,并且xml文件聲明處也會跟著改變。

引用別人答案:解釋document.setXMLEncodingformat.setEncoding設置編碼的區別

public class TestXML{   @Test    public void test() throws IOException{        Document doc = new DefaultDocument();        doc.addElement("root");        // 這里打印出來是默認的utf-8        System.out.println(doc.asXML());        doc.setXMLEncoding("utf-16");        // 這里打印出來是修改后的utf-16        System.out.println(doc.asXML());        // 這里沒有設置編碼格式默認保存的是utf-8,看一下dom4j的源碼就知道了        saveXML(doc, "D://temp//test//test1.xml", null);        // 這里設置了所以保存以后編碼格式是big5        saveXML(doc, "D://temp//test//test2.xml", "big5");    }    private void saveXML(Document doc, String filePath, String encode) throws IOException{        OutputFormat format = new OutputFormat();        if (null != encode){            format.setEncoding(encode.toUpperCase());        }        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filePath),format);        xmlWriter.write(doc);        xmlWriter.flush();        xmlWriter.close();    }}

上面代碼出自此處。

最后要說一下:

XMLWriter可以傳入OutputStream或者WriterXMLWriter writer = new XMLWriter(OutputStream, OutputFormat);XMLWriter writer = new XMLWriter(Writer, OutputFormat);最初試著傳入了new FileWriter(file),如下try {XMLWriter writer = new XMLWriter(new FileWriter(f), format);writer.write(document);writer.close();result = fileName;} catch (IOException e) { // TODO Auto-generated catch blocke.printStackTrace();}但是得到的結果并不對。修改為如下后,結果正確。try {OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(f), format);xmlWriter.write(document);xmlWriter.flush();xmlWriter.close();result = fileName;} catch (IOException e) { // TODO Auto-generated catch blocke.printStackTrace();LOG.error("trans for XML error:", e);}

記錄。

相關鏈接:http://m.survivalescaperooms.com/problems/64178http://bbs.csdn.net/topics/370057777http://liuchunqing2001.blog.163.com/blog/static/3082291201382911214196/http://lavasoft.blog.51cto.com/62575/235272http://www.educity.cn/wenda/105197.htmlhttp://www.blogjava.net/i369/articles/154264.htmlhttp://bbs.csdn.net/topics/290027113http://developer.51cto.com/art/200903/117512.htmhttp://pridesnow.VEvb.com/blog/561958http://blog.csdn.net/chenghui0317/article/details/11486271Java創建xml文檔筆記(DOM,DOM4J)http://m.survivalescaperooms.com/lanxuezaipiao/archive/2013/05/17/3082949.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新乡县| 徐汇区| 绿春县| 罗山县| 辉南县| 凌海市| 新邵县| 南昌市| 沈阳市| 商城县| 安多县| 科技| 南涧| 格尔木市| 泸定县| 绩溪县| 贡山| 霸州市| 镇巴县| 红河县| 包头市| 临泽县| 怀宁县| 墨竹工卡县| 会宁县| 辰溪县| 华阴市| 墨竹工卡县| 堆龙德庆县| 赤水市| 中江县| 略阳县| 大悟县| 徐州市| 东台市| 桑植县| 五寨县| 云和县| 沙坪坝区| 安平县| 澜沧|