dom4j内存中格式化

public static String formatXML(Document doc)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    StringWriter localStringWriter = null;
    XMLWriter localXMLWriter = null;
    OutputFormat localOutputFormat = null;
    try
    {
      localOutputFormat = OutputFormat.createPrettyPrint();
      localXMLWriter = new XMLWriter(localOutputFormat);
      localStringWriter = new StringWriter();
      localXMLWriter.setWriter(localStringWriter);
      localXMLWriter.write(doc);
      BufferedReader localBufferedReader = new BufferedReader(new StringReader(localStringWriter.toString()));
      for (String str = localBufferedReader.readLine(); str != null; str = localBufferedReader.readLine())
      {
        if (str.length() <= 0)
          continue;
        localStringBuffer.append(str + "\n");
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      localStringWriter = null;
      localXMLWriter = null;
      localOutputFormat = null;
    }
    return localStringBuffer.toString();
  }

你可能感兴趣的:(dom4j)