码迷,mamicode.com
首页 > 其他好文 > 详细

String转XML

时间:2018-04-03 12:46:10      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:class   format   output   tac   doc   row   dom4j   return   post   

 /**
     * 
     * 格式化String为Xml
     * 
     * @param inputXML
     * @return
     * @throws Exception
     */
    public static String formatXml(String inputXML) throws Exception {
        String xml = null;
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        org.dom4j.Document document = reader.read(new StringReader(inputXML));
        try {
            if (document != null) {
                StringWriter stringWriter = new StringWriter();
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setNewLineAfterDeclaration(false);
                writer = new XMLWriter(stringWriter, format);
                writer.write(document);
                writer.flush();
                xml = stringWriter.getBuffer().toString();
            }
        } finally {
            if (writer != null) {
                try {
                    writer.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return xml;
    }

 

String转XML

标签:class   format   output   tac   doc   row   dom4j   return   post   

原文地址:https://www.cnblogs.com/yuyuchen/p/8707567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!