รหัสหลัก:
แพ็คเกจ com.ddatsh; นำเข้า java.io.ioException; นำเข้า java.io.stringReader; นำเข้า java.io.stringwriter; นำเข้า java.io.unsupportencodexception; นำเข้า org.dom4j.document; นำเข้า org.dom4j.documentexception; นำเข้า org.dom4j.io.outputformat; นำเข้า org.dom4j.io.saxreader; นำเข้า org.dom4j.io.xmlwriter; คลาสสาธารณะ XMLFORMAT {รูปแบบสตริงคงที่สาธารณะ (String Str) พ่นข้อยกเว้น {SaxReader reader = new SaxReader (); // system.out.println (ผู้อ่าน); // ความคิดเห็น: สร้างสตริงของสตรีมอินพุตอักขระ stringreader ใน = new StringReader (str); เอกสารเอกสาร = reader.read (in); // system.out.println (doc.getRootelement ()); // ความคิดเห็น: สร้างรูปแบบเอาต์พุตรูปแบบ outputFormat = outputFormat.CreatePretTyPrint (); // formater = outputFormat.createCompactFormat (); // ความคิดเห็น: ตั้งค่าการเข้ารหัสเอาต์พุตของ XML formatter.setEncoding ("UTF-8"); // ความคิดเห็น: สร้าง output (เป้าหมาย) stringWriter out = new StringWriter (); // ความคิดเห็น: สร้างสตรีมเอาท์พุท XMLWriter Writer = New XMLWriter (ออก, Formatter); // ความคิดเห็น: สตริงที่จัดรูปแบบเอาต์พุตลงในเป้าหมายและหลังจากดำเนินการ สตริงที่จัดรูปแบบจะถูกบันทึกไว้ใน OUT Writer.Write (DOC); Writer.close (); System.out.println (out.toString ()); // ความคิดเห็น: ส่งคืนผลลัพธ์ที่จัดรูปแบบของเรากลับออกมา toString (); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่นข้อยกเว้น {string head = "<? xml version =/" 1.0/"encoding =/" gbk/"?>"; string str = "<equestdata> <headdata> <sererCode> sh1_admin </uSerCode> <sername> sh1_admin </username> <serercompanycode> 3107 </usercompanycode> พิมพ์> </headdata> <bodydata> <serfortId> 113100000033 </reportId> <surant> a5rfg87 </surant> <numberplate> Shanghai E78612 </numbertaple> 15:07:00 </accidentdate> <pregorsDate> 2011-02-25 15:07:00 </reportdate> <Frovince> 310000 </furnince> <itity> 310100 </ity> <cident> </istrict> <ciscidplace> cididentlatitude> <Evarveylongitude> </evelylongitude> <everveylatitude> </earveylatitude> <SincenereportFlag> </scenereportflag> Surveyplace> <peratorId> 3525 </peratorId> <peratorname> sh_admin </peratorname> <somverydealid> 30000800 </reportdealid> CustomerTypecode> </customerTypecode> <ForcePolicyID> A5RFG87A5RFG87A5RFG87 </ForcePolicyID> <bizpolicyId> </bizpolicyId> <dExd> 0 </index> // system.out.println (str); รูปแบบ (str); -เครื่องมือการจัดรูปแบบออนไลน์ที่ใช้โดย Oschina ใช้รหัสนี้