首页 > 学院 > 开发设计 > 正文

dom4j中文问题解决方法。

2019-11-18 11:21:51
字体:
来源:转载
供稿:网友

1、文件形式:
  xmlWriter writer = null;

         /** 格式化输出,类型IE浏览一样 */

         OutputFormat format = OutputFormat.createPRettyPrint();

         /** 指定XML编码 */

         format.setEncoding("GBK");
try{
        writer= new XMLWriter(new FileWriter(new File("test.xml")),format);//用FileOutputStream更好。
        writer.write(document);

       writer.close();    
 }
catch(IOException ioe){ioe.printStackTrace();}

 2、String形式,我现在做的框架中,是PO<-->XML--Servlet,不需要生成文件,搜索了半天没搜索出来,后来干脆自己试出来了。
StringWriter sw=new StringWriter();
XMLWriter writer = null;

  OutputFormat format = OutputFormat.createPrettyPrint();

 

    format.setEncoding("GBK");
writer=new XMLWriter(format);
 writer.setWriter(sw);
 writer.write(document);
 System.out.println(sw.toString());



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表