package com.dw.lib;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class JavaXMLIndent {

	public static String prettyFormat(String input) {
	    return prettyFormat(input, "2");
	}

	public static String prettyFormat(String input, String indent) {
	    Source xmlInput = new StreamSource(new StringReader(input));
	    StringWriter stringWriter = new StringWriter();
	    try {
	        TransformerFactory transformerFactory = TransformerFactory.newInstance();
	        Transformer transformer = transformerFactory.newTransformer();
	        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent);
	        transformer.transform(xmlInput, new StreamResult(stringWriter));

	        String pretty = stringWriter.toString();
	        pretty = pretty.replace("\r\n", "\n");
	        return pretty;              
	    } catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	public void FormatXML(String inputfile, String outputFile) throws IOException {
		FileUtils fileUtils = new FileUtils();
		fileUtils.stringToFile(prettyFormat(fileUtils.fileToString(inputfile)),outputFile);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		JavaXMLIndent xmlIndent = new JavaXMLIndent();
		//String formattedJson1="";
		try {
			xmlIndent.FormatXML("/Users/di.wu/test/1.xml", "/Users/di.wu/test/2.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

@@ -14,16 +14,70 @@
 w.lib;%0A%0A
+import java.io.File;%0Aimport java.io.FileOutputStream;%0A
 import j
@@ -155,16 +155,107 @@
 riter;%0A%0A
+import javax.xml.parsers.DocumentBuilder;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0A
 import j
@@ -399,32 +399,74 @@
 sformerFactory;%0A
+import javax.xml.transform.dom.DOMSource;%0A
 import javax.xml
@@ -546,16 +546,78 @@
 ource;%0A%0A
+import org.w3c.dom.Document;%0Aimport org.xml.sax.InputSource;%0A%0A
 public c
@@ -645,38 +645,36 @@
 %0A%09public static 
-String
+void
  prettyFormat(St
@@ -687,160 +687,300 @@
 nput
-) %7B%0A%09    return prettyFormat(input, %222%22);%0A%09%7D%0A%0A%09public static String prettyFormat(String input, String indent) %7B%0A%09    Source xmlInput = new Stream
+, String output) %7B    %0A%09    try %7B%0A%09    %09FileUtils f = new FileUtils();%0A%09    %09DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();%0A%09    %09factory.setNamespaceAware(true);%0A%09    %09DocumentBuilder db = factory.newDocumentBuilder();%0A%09%09    Document doc = db.parse( new Input
 Source(
+ 
 new 
@@ -996,80 +996,90 @@
 der(
-input));%0A%09    StringWriter stringWriter = new StringWriter(
+ f.fileToString(input) ) ) ); %0A%09%09    DOMSource source = new DOMSource(doc
 );%0A
+%09
 %09    
-try %7B
 %0A%09  
@@ -1154,24 +1154,99 @@
 Instance();%0A
+%09        transformerFactory.setAttribute(%22indent-number%22, new Integer(2));%0A
 %09        Tra
@@ -1363,32 +1363,105 @@
 INDENT, %22yes%22);%0A
+%09        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,%22yes%22);%0A
 %09        transfo
@@ -1537,91 +1537,114 @@
     
-transformer.setOutputProperty(%22%7Bhttp://xml.apache.org/xslt%7Dindent-amount%22, indent
+%0A%09        File file = new File(output); %0A%09        if(!file.exists())%7B %0A%09            file.createNewFile(
 );
+ 
 %0A%09  
@@ -1653,73 +1653,82 @@
     
-transformer.transform(xmlInput, new StreamResult(stringWriter));%0A
+%7D %0A%09        %0A%09        FileOutputStream out1 = new FileOutputStream(file); 
 %0A%09  
@@ -1737,104 +1737,76 @@
     
-String pretty = stringWriter.toString();%0A%09        pretty = pretty.replace(%22%5Cr%5Cn%22, %22%5Cn%22
+ %0A%09        StreamResult xmlResult = new StreamResult(out1
 );
+ 
 %0A%09      
   re
@@ -1805,26 +1805,61 @@
     
-  return pretty;  
+%0A%09        transformer.transform(source, xmlResult); %0A
     
@@ -2031,16 +2031,19 @@
 ion %7B%0A%09%09
+// 
 FileUtil
@@ -2079,66 +2079,20 @@
 ;%0A%09%09
-fileUtils.stringToFile(prettyFormat(fileUtils.fileToString
+prettyFormat
 (inp
@@ -2097,18 +2097,17 @@
 nputfile
-))
+ 
 ,outputF

update using TransformerFactory