Core code:
package ; import ; import ; import ; import ; import org.; import org.; import org.; import org.; import org.; public class XmlFormat { public static String format(String str) throws Exception { SAXReader reader = new SAXReader(); // (reader); // Comment: Create a string of character input streams StringReader in = new StringReader(str); Document doc = (in); // (()); // Comment: Create output format OutputFormat formater = (); //formater=(); // Comment: Set the output encoding of xml ("utf-8"); // Comment: Create output (target) StringWriter out = new StringWriter(); // Comment: Create output stream XMLWriter writer = new XMLWriter(out, formater); // Comment: Output the formatted string into the target and after execution. The formatted string is saved in out. (doc); (); (()); // Comment: Return our formatted results return (); } public static void main(String[] args) throws Exception { String head="<?xml version=\"1.0\" encoding=\"GBK\"?>"; String str = "<RequestData><HeadData><UserCode>sh1_admin</UserCode><UserName>sh1_admin</UserName><UserCompanyCode>3107</UserCompanyCode><UserCompanyName>Shanghai Branch 1</UserCompanyName><RequestType>03</Request Type></HeadData><BodyData><ReportId>113100000033</ReportId><Insurant>a5rfg87</Insurant><NumberPlate>Shanghai E78612</NumberPlate><EngineModel></EngineModel><CarVin></CarVin><AccidentDate>2011-02-25 15:07:00</AccidentDate><ReportDate>2011-02-25 15:07:00</ReportDate><Province>310000</Province><City>310100</City><District></District><AccidentPlace>1</AccidentPlace><AccidentLongitude></AccidentLongitude><AccidentLatitude></Ac cidentLatitude><SurveyLongitude></SurveyLongitude><SurveyLatitude></SurveyLatitude><SceneReportFlag></SceneReportFlag><Reporter></Reporter><ReporterTel></ReporterTel><SurveyPlace></ SurveyPlace><OperatorId>3525</OperatorId><OperatorName>sh_admin</OperatorName><ReportDealId>30000800</ReportDealId><ReportDealName>Jiangsu Branch</ReportDealName><CompanyName></CompanyName>< CustomerTypeCode></CustomerTypeCode><ForcePolicyId>a5rfg87a5rfg87a5rfg87</ForcePolicyId><BizPolicyId></BizPolicyId><Index>0</Index><FieldName>5</FieldName></BodyData></RequestData>"; // (str); format(str); } }
The online formatting tool used by Oschina uses this code.