dom4j格式化xml文件
程序员文章站
2022-06-13 09:09:27
...
dom4j格式xml并输出为*.xml文件
dom4j格式化xml并格式化输出
public static void main(String[] args) { Document document = DocumentHelper.createDocument(); Element employees=document.addElement("employees"); Element employee=employees.addElement("employee"); Element name= employee.addElement("name"); name.setText("jim"); Element sex=employee.addElement("sex"); sex.setText("male"); Element age=employee.addElement("age"); age.setText("29"); try { OutputFormat format = new OutputFormat(); format.setIndent(true); format.setNewlines(true); Writer fileWriter=new FileWriter("F:/test.xml"); XMLWriter xmlWriter=new XMLWriter(fileWriter,format); xmlWriter.write(document); xmlWriter.close(); } catch (IOException e) { System.out.println(e.getMessage()); } }
dom4j格式化xml并格式化输出
public static String formatXml(String str) throws DocumentException, IOException { SAXReader reader=new SAXReader(); StringReader in=new StringReader(str); Document doc=reader.read(in); OutputFormat formater=OutputFormat.createPrettyPrint(); formater.setEncoding("UTF-8"); StringWriter out=new StringWriter(); XMLWriter writer=new XMLWriter(out,formater); writer.write(doc); writer.close(); return out.toString(); }