日期:2014-05-17  浏览次数:20550 次

格式化一个xml文件在html中显示

public static String format(String str)
??? ??? ??? throws UnsupportedEncodingException, IOException, DocumentException {

??? ??? SAXReader reader = new SAXReader();
??? ??? // System.out.println(reader);
??? ??? StringReader in = new StringReader(str);
??? ??? Document doc = reader.read(in);
??? ??? // System.out.println(doc.getRootElement());
??? ??? OutputFormat formater = OutputFormat.createPrettyPrint();
??? ??? formater.setEncoding("utf-8");
??? ??? StringWriter out = new StringWriter();
??? ??? XMLWriter writer = new XMLWriter(out, formater);
??? ??? writer.write(doc);

??? ??? // writer.close();
??? ??? System.out.println(out.toString());
??? ??? return out.toString();
??? }