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());
新聞熱點
疑難解答