핵심 코드 :
패키지 com.ddatsh; import java.io.ioexception; import java.io.stringReader; import java.io.stringwriter; import java.io.unsupportedencodingException; 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)은 예외 {saxreader reader = new SaxReader (); // System.out.println (Reader); // 댓글 : 문자열 입력 스트림 스트림 스트림 스트림 stringReader in = new StringReader (str); 문서 doc = reader.read (in); // system.out.println (doc.getRootElement ()); // 댓글 : 출력 형식 생성 outputformat format = outputformat.createPrettyPrint (); // Formater = outputformat.createCompactFormat (); // 댓글 : XML Formatter의 출력 인코딩을 설정합니다. // 댓글 : 출력 생성 (대상) stringWriter out = new StringWriter (); // 댓글 : 출력 스트림 생성 xmlwriter writer = new xmlwriter (out, formatter); // 주석 : 대상과 실행 후 출력 형식 문자열. 형식화 된 문자열은 아웃에 저장됩니다. Writer.write (Doc); Writer.close (); System.out.println (out.toString ()); // 댓글 : 형식의 결과를 반환합니다. return.toString (); } public static void main (string [] args)은 예외 {string head = "<? xml 버전 =/"1.0/"encoding =/"gbk/"?>"; String str = "<requestData> <HeadData> <usercode> SH1_ADMIN </userCode> <ERSERNAME> SH1_ADMIN </username> <UserCompanyCode> 3107 </userCompanyCode> <userCompanyName> Shanghai Branch 1 </userCompanYame> <requestType> 03 </요청 type> </headdata> <boddata> <perglid> 113100000033 </reportid> <surant> a5rfg87 </insurant> <numberplate> Shanghai e78612 </numberplate> <Enginemodel> </enginemodel> <carvin> </carvin> 2011-02-25 15:07:00 </miscrenceDate> <ReportDate> 2011-02-25 15:07:00 </reportDate> <Province> 310000 </province> <city> 310100 </city> </District> <ScrencePlace> 1 </사고 장소> <Scountlitude> </misuboudlongitude> </ac cidentlatitude> <surveylonditude> </surveylonditude> <surveylatitude> </surveylatitude> <scenereportflag> </sineereportflag> <Reporter> </reporter> <preportertel> </reportertel> <surverplace> </ SurveyPlace> <operatorId> 3525 </OperatorId> <operatorName> SH_ADMIN </OPERATORNAME> <PREPORDEALID> 30000800 </reportDealId> <reportDealName> jiangsu branch </reportDealName> <companyName> </companyName> < CustomerTyPecode> </customerTyPecode> <ForcepolicyId> A5RFG87A5RFG87A5RFG87 </forcepolicyid> <bizpolicyid> </bizpolicyid> <index> 0 </index> <FieldName> 5 </fieldName> </bodydata> </requestData> "; // system.out.println (str); 형식 (str); }}Oschina에서 사용하는 온라인 서식 도구는이 코드를 사용합니다.