格式化xml

作者: 明日未期 | 来源:发表于2019-11-24 09:53 被阅读0次
    jar dom4j-1.6.1.jar
    import java.io.StringWriter;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    
    public class FormatXml{
        public static String formatXml(String xml){
            String res = null;
            try{
                Document doc = DocumentHelper.parseText(xml);
                OutputFormat format = OutputFormat.createPrettyPrint();
                StringWriter writer = new StringWriter();
                XMLWriter xmlWriter = new XMLWriter(writer,format);
                xmlWriter.write(doc);
                xmlWriter.close();
                res = writer.toString().trim();
            }catch(Exception e){}
            return res;
        }
    }
    

    相关文章

      网友评论

        本文标题:格式化xml

        本文链接:https://www.haomeiwen.com/subject/mbxlwctx.html