格式化XML


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.StringWriter;

public class XmlUtil {

    public static String formatXml(String xml) throws Exception
    {
        Document document = DocumentHelper.parseText(xml);
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, format);
        xmlWriter.write(document);
        xmlWriter.close();
        return writer.toString();
    }
}

你可能感兴趣的:(java,xml,格式化)