XML格式化

/**
*
* XML格式化
* @param inputXML
* @return
* @throws Exception
*/
private  String formatXML(String inputXML) throws Exception { 
        SAXReader reader = new SAXReader(); 
        Document document = reader.read(new StringReader(inputXML)); 
        String requestXML = null; 
        XMLWriter writer = null; 
        if (document != null) { 
          try { 
            StringWriter stringWriter = new StringWriter(); 
            OutputFormat format = new OutputFormat(" ", true); 
            writer = new XMLWriter(stringWriter, format); 
            writer.write(document); 
            writer.flush(); 
            requestXML = stringWriter.getBuffer().toString(); 
          } finally { 
            if (writer != null) { 
              try { 
                writer.close(); 
              } catch (IOException e) { 
              } 
            } 
          } 
        } 
        return requestXML; 
      } 

猜你喜欢

转载自zhouguofeng.iteye.com/blog/2300194