//如下为解析代码 public static void main(String[] args) { String str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<data name=\"SYS_HEAD\">" + "<struct>" + "</struct>" + "</data>" ; SAXReader reader=new SAXReader(); StringReader in=new StringReader(str); Document doc; StringWriter out=new StringWriter(); try { doc = reader.read(in); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); format.setIndent(true); format.setIndent(" "); format.setNewlines(true); try { XMLWriter writer = new XMLWriter(out, format); writer.write(doc); writer.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print(out.toString()); } //输入后结果:
<?xml version="1.0" encoding="UTF-8"?> <data name="SYS_HEAD"> <struct/> </data>
//问题,不管如何格式化,输出结果中的第一行与第三行之间这行都去不掉,不知道大家有没有遇到,求解决方法!!!