เมื่อเร็ว ๆ นี้ฉันได้ทำงานกับเฟรมเวิร์กแพลตฟอร์มที่มีประสิทธิภาพในความเป็นจริงเมื่อฉันรู้ว่าฉันต้องการทำกรอบง่ายๆหลังจากที่ฉันคิดว่าฉันคิดว่าสิ่งที่ฉันอยากทำ การแสดงผลหน้าแรกที่กำหนดค่าได้ แต่หลังจากทำงานหนักเป็นเวลาสองสัปดาห์อาจเป็นหลังจากใช้งานฟังก์ชั่นฉันรู้ว่านี่ไม่ใช่สิ่งที่ฉันต้องการทำ สัปดาห์ยังไม่สูญเปล่าและฉันได้เรียนรู้มากมายจากมัน
เมื่อสร้างระบบฉันมักจะพบกับความจำเป็นในการอ่าน XML เกือบจะเหมือนกันฉันพบว่าทำไมฉันไม่สรุปการดำเนินการทั้งหมดเหล่านี้ลงในชั้นเรียนและเรียกพวกเขาโดยตรงเมื่อใช้พวกเขา ด้วยความคิดนี้ยังคงตรวจสอบออนไลน์ (เพราะฉันรู้สึกว่าต้องมีอะไรออนไลน์ถ้าฉันห่อหุ้มด้วยตัวเองหัวของฉันจะได้รับน้ำจริงๆ)
การแสดงซอร์สโค้ด:
การคัดลอกรหัสมีดังนี้:
แพ็คเกจ com.gxpt.struts2;
นำเข้า Java.io.File;
นำเข้า java.io.filewriter;
นำเข้า java.util.iterator;
นำเข้า java.util.list;
นำเข้า java.util.map;
นำเข้า org.dom4j.attribute;
นำเข้า org.dom4j.document;
นำเข้า org.dom4j.documentException;
นำเข้า org.dom4j.documenthelper;
นำเข้า org.dom4j.element;
นำเข้า org.dom4j.io.OutputFormat;
นำเข้า org.dom4j.io.saxReader;
นำเข้า org.dom4j.io.xmlwriter;
ระดับสาธารณะ testadddeletexml {
เอกสารเอกสารส่วนตัว
String Private FilePath;
// private writelog writelog;
โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น documentException {
// String filePath = system.getProperty ("user.dir")+"/xmlfiles/localservicesconfig.xml";
String filePath = "f: // Java Project // Platform University // Demo // gxpt // webcontent // limits // manager.txt";
TestAddDeleteXML Operator = ใหม่ TestAddDeleteXML (FilePath);
operator.getxmlfile ();
// แผนที่แผนที่ = ใหม่ hashmap ();
//map.put("id "," m1 ");
//map.put("name","module1 ");
//map.put("url "," index1.jsp ");
//operator.addchild("div "," div9 "," โมดูล "," ", แผนที่);
//operator.updatechild("style "," "," div "," asdfasdf ", 1);
Operator.DeleteChildone ("Style", "", "Div", "Div11");
//operator.deletechild("div "," div9 "," โมดูล ");
// string str = operator.getChild ("div", "div8", "โมดูล");
//system.out.println(str);
// element root = document.getRootelement (); // รับชื่อโหนดรูท
-
สาธารณะ testadddeletexml (String filepath) {
this.document = null;
this.filepath = filePath;
// writelog = new writelog ();
-
-
* สร้างไฟล์ XML
* @param rootname: ชื่อโหนดรูท
-
โมฆะสาธารณะ createxmlfile (String rootname) {
if (! fileexist ()) {
this.document = documenthelper.createDocument ();
this.document.addelement (rootname);
savexmlfile (this.document);
-
-
-
* รับเอกสาร XML ที่มีอยู่
* @กลับ
-
เอกสารสาธารณะ getxmlfile () {
if (fileexist ()) {
saxReader reader = new SaxReader ();
พยายาม {
this.document = reader.read (ไฟล์ใหม่ (FilePath));
} catch (documentexception e) {
// สตริง loginfo = stacktracetoString.getExceptionTrace (e);
// writelog.writeLogtoend ("localserverManager", loginfo);
} ในที่สุด{
ผู้อ่าน = null;
-
} อื่น {
// การเขียนบันทึก
// string loginfo = "ไฟล์ XML ไม่มีอยู่อ่านข้อผิดพลาด!";
// writelog.writeLogtoend ("localserverManager", loginfo);
System.Exit (0);
-
ส่งคืนเอกสารนี้;
-
-
* เพิ่มองค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อโหนดที่จะเพิ่ม
* @param childValue: ค่าโหนดที่จะเพิ่ม
-
โมฆะสาธารณะ addChild (String fathernode, String fatherattr, String ChildName, String ChildValue, MAP MAPATTR) {
เด็ก (fathernode, fatherattr, ชื่อเด็ก, เด็ก, "เพิ่ม", mapattr, 0);
-
-
* แก้ไของค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อโหนดที่จะแก้ไข
* @param childValue: ค่าโหนดที่จะแก้ไข
-
โมฆะสาธารณะ updateChild (String fathernode, String fatherattr, String ChildName, String ChildValue, IntupertId) {
ChildOperator (Fathernode, Fatherattr, ชื่อเด็ก, เด็ก, "อัปเดต", null, updatid);
-
-
* ลบองค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อของโหนดที่จะลบ
-
โมฆะสาธารณะ deleteChild (String fathernode, String fatherattr, String ChildName) {
เด็ก (fathernode, fatherattr, เด็ก, "", "ลบ", null, 0);
-
-
* ลบองค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อของโหนดที่จะลบ
-
โมฆะสาธารณะ deleteChildall (String fathernode, String fatherattr, String ildName) {
เด็ก (fathernode, fatherattr, เด็ก, "", "deleteall", null, 0);
-
-
* ลบองค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อของโหนดที่จะลบ
-
โมฆะสาธารณะ deleteChildone (String fathernode, String fatherattr, String ChildName, String ChildValue) {
เด็ก (fathernode, fatherattr, ชื่อเด็ก, เด็ก, "deleteone", null, 0);
-
-
* รับค่าขององค์ประกอบ
* @param fatherpath: ชื่อโหนดหลัก
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: ชื่อของโหนดที่จะลบ
-
Public String getChild (String fathernode, String fatherattr, String ildName) {
สตริงผลลัพธ์ = "";
ผลลัพธ์ = ChildOperator (fathernode, fatherattr, ชื่อเด็ก, "", "get", null, 0);
ผลการกลับมา;
-
-
* การทำงานของโหนดเด็ก
* @param fathernode: ชื่อโหนดพาเรนต์
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: โหนดที่จะแก้ไข
* @param ChildValue: ค่าโหนดที่แก้ไขแล้ว
* @Param Operator: ชื่อของการดำเนินการที่จะดำเนินการ
-
สตริงที่ซิงโครไนซ์ส่วนตัว ChildOperator (String fathernode, String fatherattr, String ildName, String ChildValue, Operator String, Map Mapattr, IntupertEid) {
สตริงผลลัพธ์ = "";
if (this.document == null) {
กลับ "null";
-
element root = this.document.getRootelement (); // รับชื่อโหนดรูท
if (! root.getName (). เท่ากับ (fathernode)) {// ถ้ามันไม่ได้เพิ่มภายใต้โหนดรูท
ผลลัพธ์ = xmlelementoperator (รูท, fathernode, fatherattr, ชื่อเด็ก, ค่าเด็ก, ผู้ประกอบการ, Mapattr);
}อื่น{
if (operator.equals ("เพิ่ม")) {
Element ChildElement = root.addelement (ชื่อเด็ก); // ค่าแอตทริบิวต์องค์ประกอบไม่มีอยู่ในโหนดรูท
childelement.setAttributeValue ("id", ค่าเด็ก);
savexmlfile (this.document);
} else if (operator.equals ("update")) {
รายการเด็ก = root.elements (ชื่อเด็ก);
// สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
// องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
// ทุกคน
// ทุกคน SetAtattributeValue ("id", เด็ก);
องค์ประกอบทุกคน = (องค์ประกอบ) childelements.get (updateId);
ทุกคน SetAttributeValue ("ID", เด็ก);
-
savexmlfile (this.document);
} อื่นถ้า (operator.equals ("ลบ")) {
แสดงรายการเด็ก = root.elements (ชื่อเด็ก); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
แสดงรายการ Childrenes1 = ทุกคน Elements ("Module");
สำหรับ (iterator children1 = childelements1.iterator (); childs1.hasnext ();) {
องค์ประกอบทุกคน 1 = (องค์ประกอบ) childs1.next ();
ทุกคนลบ (ทุกคน 1);
-
-
savexmlfile (this.document);
} อื่นถ้า (operator.equals ("รับ")) {
แสดงรายการเด็ก = root.elements (ชื่อเด็ก); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
ผลลัพธ์ = ทุกคน getText ();
-
savexmlfile (this.document);
} อื่นถ้า (operator.equals ("deleteone")) {
แสดงรายการเด็ก = root.elements (ชื่อเด็ก); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
String Divelement = Every.AttributeValue ("id");
if (divlement.equals (เด็ก)) {
Root.remove (ทุกคน);
-
-
savexmlfile (this.document);
} else if (operator.equals ("deleteAll")) {
แสดงรายการเด็ก = root.lements (); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
รายการ childdiv = ทุกคน Elements ();
สำหรับ (iterator childrendiv = childdiv.iterator (); childsdiv.hasnext ();) {
องค์ประกอบ EveryOnediv = (องค์ประกอบ) childsdiv.next ();
ทุกคนขจัด (Everyonediv);
-
-
-
savexmlfile (this.document);
-
ผลการกลับมา;
-
-
* การดำเนินการองค์ประกอบแบบเรียกซ้ำ
* องค์ประกอบ @param: องค์ประกอบที่จะเรียกซ้ำ
* @param fathernode: ชื่อโหนดพาเรนต์
* @param fatherattr: แอตทริบิวต์โหนดแม่
* @Param ChildName: โหนดที่จะดำเนินการ
* @param ChildValue: ค่าโหนดหลังการทำงาน
* @Param Operator: ชื่อของการดำเนินการที่จะดำเนินการ
-
สตริงซิงโครไนซ์ส่วนตัว XMLElementOperator (องค์ประกอบองค์ประกอบ, สตริง fathernode, สตริง fatherattr, สตริงเด็ก, สตริงเด็ก, ตัวดำเนินการสตริง, แผนที่ mapattr) {
สตริงผลลัพธ์ = "";
รายการองค์ประกอบ = element.lements ();
สำหรับ (iterator it = elements.iterator (); it.hasnext ();) {
องค์ประกอบ currentElement = (องค์ประกอบ) it.next ();
if (! currentElement.getName (). เท่ากับ (fathernode)) {// ดำเนินการค้นหาต่อเมื่อองค์ประกอบปัจจุบันไม่ใช่องค์ประกอบหลักที่เรากำลังมองหา
XmlelementOperator (CurrentElement, Fathernode, Fatherattr, ชื่อเด็ก, ค่าเด็ก, ผู้ประกอบการ, MAPATTR); // การโทรแบบเรียกซ้ำ
}อื่น{
ถ้า (currentElement.attributeCount ()> 0) {// เมื่อองค์ประกอบปัจจุบันมีค่าแอตทริบิวต์มันคือ
สำหรับ (iterator list = currentElement.attributeIterator (); list.hasnext ();) {// transf ค่าแอตทริบิวต์
Attribute attr = (attribute) list.next ();
if (attr.getValue (). เท่ากับ (fatherattr)) {// กำหนดองค์ประกอบหลักที่ไม่ซ้ำกันตามค่าแอตทริบิวต์
if (operator.equals ("เพิ่ม")) {// เพิ่มองค์ประกอบ
Element ChildElement = CurrentElement.addelement (ชื่อเด็ก);
childelement.settext (ChildValue);
ตัววนซ้ำ itmapattr = mapattr.keyset (). iterator ();
ในขณะที่ (itmapattr.hasnext ()) {
สตริงคีย์ = (สตริง) itmapattr.next ();
ค่าสตริง = mapattr.get (คีย์) .toString ();
ChildElement.setAttributeValue (คีย์, ค่า);
-
// ChildElement.setAttributeValue ("ID", "M1");
// childelement.setAttributeValue ("ชื่อ", "module1");
// ChildElement.setAttributeValue ("url", "index1.jsp");
} else if (operator.equals ("update")) {// แก้ไของค์ประกอบ
แสดงรายการเด็ก = currentElement.Elements (ชื่อเด็ก); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
ทุกคน SETTEXT (ChildValue); // แก้ไขค่าขององค์ประกอบนี้
-
} else if (operator.equals ("ลบ")) {// ลบองค์ประกอบที่ระบุ
แสดงรายการเด็ก = currentElement.lements (); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
currentElement.remove (ทุกคน);
-
} อื่นถ้า (operator.equals ("รับ")) {
แสดงรายการเด็ก = currentElement.Elements (ชื่อเด็ก); // รับโหนดลูกทั้งหมดภายใต้โหนดปัจจุบันตัดสินค่าของพวกเขาและแก้ไขพวกเขา
สำหรับ (iterator children = childlements.iterator (); childs.hasnext ();) {
องค์ประกอบทุกคน = (องค์ประกอบ) childs.next ();
// result = ทุกคน getText ();
ผลลัพธ์ = ทุกคน. attributeValue ("id")+","+ผลลัพธ์;
-
-
อื่น{
// การเขียนบันทึก
// สตริง loginfo = "ตัวดำเนินการ xmlfile ไม่มีอยู่!";
// writelog.writeLogtoend ("localserverManager", loginfo);
-
-
-
-
-
-
savexmlfile (this.document);
ผลการกลับมา;
-
-
* บันทึกไฟล์ XML
* @Param Document: XML ชื่อไฟล์
-
โมฆะส่วนตัว savexmlfile (เอกสารเอกสาร) {
พยายาม {
รูปแบบ outputFormat = outputFormat.CreatePretTyPrint ();
format.setEncoding ("UTF-8");
XMLWriter Writer = New XMLWriter (FileWriter ใหม่ (ไฟล์ใหม่ (FilePath)), รูปแบบ);
Writer.write (เอกสาร);
Writer.close ();
} catch (Exception e) {
// สตริง loginfo = stacktracetoString.getExceptionTrace (e);
// writelog.writeLogtoend ("localserverManager", loginfo);
-
-
-
* ตรวจสอบว่ามีไฟล์ XML อยู่หรือไม่
* @param ชื่อไฟล์
* @กลับ
-
บูลีนส่วนตัว fileexist () {
java.io.file objfile = ใหม่ java.io.file (this.filepath);
if (objfile.exists ()) {
กลับมาจริง;
} อื่น {
กลับเท็จ;
-
-
-
ไฟล์ XML:
การคัดลอกรหัสมีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?>?
<style>
<div id = "div8">
<module id = "m1" name = "module1" url = "index1.jsp"/>>>>> >>
<module id = "m2" name = "module2" url = "index2.jsp"/>>>> >>
<module id = "m3" name = "module3" url = "index3.jsp"/>>>>> >>
</div>
<div id = "div9">
<module id = "m9" name = "module9" url = "index3.jsp"/>>>> >>
<module id = "m10" name = "module10" url = "index4.jsp"/>>>>>> >>
<module id = "m11" name = "module11" url = "index5.jsp"/>>>>
</div>
</style>
การวิเคราะห์: ที่นี่เราใช้วิธีการเรียกซ้ำเพื่อตรวจสอบว่าการดำเนินการสำหรับโหนดหรือโหนดเด็กซึ่งค่อนข้างชัดเจน ใช้การฉีดพึ่งพาอาศัยกันช่วยรักษาปัญหาหากการตัดสิน แต่ฉันได้ทำการสาธิตในเวลานั้นโดยไม่มีการปรับให้เหมาะสมมากขึ้นหากคุณสนใจคุณสามารถลองได้
สรุป: จริง ๆ แล้วมันไม่ยากที่จะอ่าน XML ในการเขียนการทำซ้ำจำนวนมากมีปัญหากับรหัสที่ต้องแก้ไขซ้ำ ๆ ดังนั้นบางครั้งแม้ว่าข้อกำหนดจะมีความสำคัญวิธีการใช้งานนั้นมีความสำคัญเท่าเทียมกัน!