ITeye博客

将xml格式的字符串进行格式化输出

javaz · 2010-05-11 15:08 · XML · 编程语言 · XML · Markdown

	public static String formatXml(String str) throws Exception{		
		Document document = null;
		document = DocumentHelper.parseText(str);
		//格式化输出格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("gb2312");
		StringWriter writer = new StringWriter();
		//格式化输出流
		XMLWriter xmlWriter = new XMLWriter(writer,format);
		//将document写入到输出流
		xmlWriter.write(document);
		xmlWriter.close();
		//输出到控制台
		System.out.println(writer.toString());
		return writer.toString();
	}

   可根据需要更改编码。

评论(1)

  1. #1 楼 · zht110227 · 2010-07-21 12:24

    编码这个东西不好搞,我这里总是有点小问题。

评论功能维护中,当前仅展示历史评论。