对xml文件格式化(主要用到了dom4j)

/**
     * @des  对xml字符串进行格式化输出
     * @param s xml文件的字符串
     * @return
     */
    public static String formatXmlString(String s)
    {
        SAXReader saxreader = new SAXReader();
        StringReader stringreader = new StringReader(s);
        org.dom4j.Document document = null;
        try
        {
            document = saxreader.read(stringreader);
        }
        catch(DocumentException documentexception)
        {
            ExceptionUtil.throwActualException(documentexception);
        }
        OutputFormat outputformat = OutputFormat.createPrettyPrint();
        outputformat.setSuppressDeclaration(true);
        StringWriter stringwriter = new StringWriter();
        XMLWriter xmlwriter = new XMLWriter(stringwriter, outputformat);
        try
        {
            xmlwriter.write(document);
        }
        catch(IOException ioexception)
        {
            ExceptionUtil.throwActualException(ioexception);
        }
        return stringwriter.toString();
    }
 

你可能感兴趣的:(xml)