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());