dom4j 格式化xml输出

            
                dom4j
                dom4j
                1.6.1
            

格式化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 格式化xml输出)