Kerncode:
Paket com.ddatsh; Import Java.io.ioException; Import Java.io.stringReader; Import Java.io.StringWriter; Import Java.io.unsupportedenenCodingException; import org.dom4j.document; import org.dom4j.documentException; import org.dom4j.io.outputformat; import org.dom4j.io.saxreader; import org.dom4j.io.xmlwriter; public class xmlformat {public static String Format (String Str) löst Ausnahme aus {SAXReader reader = new Saxreader (); // system.out.println (Leser); // Kommentar: Erstellen Sie eine Zeichenfolge von Zeichen -Eingabestreams StringReader in = new StringReader (STR); Document doc = reader.read (in); // system.out.println (doc.getrootelement ()); // Kommentar: Erstellen Sie das Ausgabebildformat OutputFormat Format = outputFormat.CreatePrettTyprint (); // Formater = outputFormat.CreateCompactFormat (); // Kommentar: Setzen Sie die Ausgabecodierung des XML-Formaters. // Kommentar: Ausgabe (Ziel) StringWriter out = new StringWriter (); // Kommentar: Erstellen Sie Ausgabestream XMLWriter Writer = New XMLWriter (OUT, Formatter); // Kommentar: Ausgabe formatiert in das Ziel und nach der Ausführung. Die formatierte Zeichenfolge wird ausgespeichert. writer.write (doc); writer.close (); System.out.println (out.toString ()); // Kommentar: Gibt unser formatiertes Ergebnis zurück. } public static void main (String [] args) löst Ausnahme aus {String head = "<? Xml Version =/" 1.0/"coding =/" gbk/"?>"; String Str = "<RequestData> <Headdata> <UsCodode> sh1_admin </userCode> <Benusername> sh1_admin </userername> <UserCompanyCode> 3107 </userCompanyCode> <UserCompanyname Type></HeadData><BodyData><ReportId>113100000033</ReportId><Insurant>a5rfg87</Insurant><NumberPlate>Shanghai E78612</NumberPlate><EngineModel></EngineModel><CarVin></CarVin><AccidentDate>2011-02-25 15:07:00 </unfalldate> <REPREITDATE> 2011-02-25 15:07:00 </reportdate> <provinz> 310000 </province> <City> 310100 </City> <doval> </District> </catchplace> 1 </catchplace> </versehentlich Cidentlatitude> <SurveyLongitude> </surveyLongitude> <surveylatitude> </surveylatitude> <ScosroeNeportFlag> </szeneportflag> <Reporter> </reporter> <reporterTel> </reporterTel> <surveyplace> </</ SurveyPlace> <OperatorID> 3525 </OperatorID> <Operatorname> sh_admin </operatorname> <REPREPTDEALID> 30000800 </reportDealId> <REPRESSEALNAME> Jiangsu Branch </reportDealName> <CompanyName> </companyName> < CustomerTypeCode> </customerTypeCode> <PocePolicyid> A5RFG87A5RFG87A5RFG87 </pocePolicyid> <bizPolicyID> </bizPolicyID> <index> 0 </index> <fieldname> 5 </fieldname> </bodyData> </raddata> </requestData> "; // system.out.println (str); Format (str); }}Das von Oschina verwendete Online -Format -Tool verwendet diesen Code.