关于xml文件格式化
用此方法的前提是你已经生成了一份xml文件,只是该文件内容不是很美观,此方法的功能仅仅是可以使其变美观而已看代码:
package com.crea.sys.xml.util;import java.io.File;import java.io.FileWriter;import org.dom4j.Document;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class XmlFormat {/** * @param args */public static void main(String[] args) {String filepath = "D:/web.xml";format(filepath);}/** ** @param filepath将指定文件xml格式化 * @return */public static void format(String filepath){File file = new File(filepath);SAXReader reader = new SAXReader();try {Document news = reader.read(file);OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new FileWriter(file),format);writer.write(news);writer.close();} catch (Exception e) {e.printStackTrace();}}}
用到的jar包:dom4j-1.6.1.jar
页:
[1]