格式化XML

 public static String format(Document document) {
  StringWriter writer = new StringWriter();

  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding("gb2312");
  XMLWriter xmlwriter = new XMLWriter(writer, format);
  try {
   xmlwriter.write(document);
  } catch (Exception e) {
   e.printStackTrace();
  }

  return writer.toString();
 }

你可能感兴趣的:(xml)