dom4j

作者: 青丝如梦 | 来源:发表于2019-08-06 10:28 被阅读0次

dom4j 格式化xml输出

            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>

格式化xml输出:

    private static String getXml() {
        String result = "";

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("smp");
        root.addElement("type").addText("java");
        root.addElement("class").addText("");

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            StringWriter stringWriter = new StringWriter();
            XMLWriter writer = new XMLWriter( stringWriter, format );
            writer.write( document );
            result = stringWriter.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }

相关文章

网友评论

      本文标题:dom4j

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