最近一直在做高效平臺的框架,其實意識到我要做一個簡單的框架的時候是在我已經做完我認為的一版介面之後,開始以為我要做的是一個可配置的首頁展示,但是吭哧吭哧做了兩個星期,大概功能實現了之後,才發現要做的不是這個,哎,需求不清楚害死人啊,但是這兩個星期並沒有白白浪費,也從中學到了很多東西,下面主要介紹讀取XML。
在做系統的時候,經常會遇到讀取xml的需求,一開始是讀取,於是我上網開始查詢讀取,接著查詢刪除,接著查詢修改,當把這些程式碼查的差不多的時候,我發現,我為什麼不把這些的操作都封裝到一個類裡,使用的時候直接呼叫,豈不是更好,感覺之前腦袋都秀逗了,分別查了那麼多,還一個個的除錯,抱著這個心態繼續上網查(因為我真心感覺網上肯定有,如果我自己封裝的話腦袋就真的進水了)。
原始碼展示:
複製程式碼 程式碼如下:
package ts2;
import ;
import Writer;
import ator;
import ;
import ;
import ibute;
import ment;
import mentException;
import mentHelper;
import ent;
import utFormat;
import eader;
import riter;
public class testAddDeleteXML {
private Document document;
private String filePath; //檔案所在的實際物理路徑
//private WriteLog writelog;
public static void main(String[] args) throws DocumentException {
//String filepath = roperty("")+"/XmlFiles/";
String filepath ="F:JAVA專案高校平臺";
testAddDeleteXML operator = new testAddDeleteXML(filepath);
mlFile();
//Map map = new HashMap();
//("id", "m1");
//("name","module1");
//("url", "");
//hild("div", "div9","module", "",map);
//teChild("style", "", "div", "asdfasdf",1);
dOne("style", "","div","div11");
//d("div", "div9","module");
//String str = hild("div", "div8", "module");
//tln(str);
//Element root = ootElement();//獲取根節點名稱
}
public testAddDeleteXML(String filepath){
ment = null;
Path = filepath;
//writelog = new WriteLog();
}
/**
* 建立XML檔案
* @param rootName:根節點名稱
*/
public void createXMLFile(String rootName) {
if(!fileExist()){
ment = teDocument();
lement(rootName);
saveXMLFile(ment);
}
}
/**
* 獲取已存在的XML文件
* @return
*/
public Document getXmlFile() {
if (fileExist()) {
SAXReader reader = new SAXReader();
try {
ment = (new File(filePath));
} catch (DocumentException e) {
// String loginfo = xceptionTrace(e);
// eLogToEnd("LocalServerManager", loginfo);
}finally{
reader = null;
}
} else {
//寫日誌
// String loginfo = "XML file does not exist,read error!";
// eLogToEnd("LocalServerManager",loginfo);
(0);
}
return ment;
}
/**
* 新增元素
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要新增的節點名稱
* @param childValue:要新增的節點值
*/
public void addChild(String fatherNode, String fatherAttr,String childName, String childValue,Map mapAttr) {
ChildOperator(fatherNode,fatherAttr,childName,childValue,"add",mapAttr,0);
}
/**
* 修改元素
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要修改的節點名稱
* @param childValue:要修改成的節點值
*/
public void updateChild(String fatherNode, String fatherAttr,String childName, String childValue,int updatId) {
ChildOperator(fatherNode,fatherAttr,childName,childValue,"update",null,updatId);
}
/**
* 刪除元素
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要刪除的節點名稱
*/
public void Child(String fatherNode, String fatherAttr,String childName) {
ChildOperator(fatherNode,fatherAttr,childName,"","",null,0);
}
/**
* 刪除元素
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要刪除的節點名稱
*/
public void ChildAll(String fatherNode, String fatherAttr,String childName) {
ChildOperator(fatherNode,fatherAttr,childName,"","All",null,0);
}
/**
* 刪除某個元素
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要刪除的節點名稱
*/
public void ChildOne(String fatherNode, String fatherAttr,String childName,String childValue) {
ChildOperator(fatherNode,fatherAttr,childName,childValue,"One",null,0);
}
/**
* 獲取某個元素數值
* @param fatherPath:父節點名稱
* @param fatherattr:父節點屬性
* @param childName:要刪除的節點名稱
*/
public String getChild(String fatherNode, String fatherAttr,String childName) {
String result = "";
result = ChildOperator(fatherNode,fatherAttr,childName,"","get",null,0);
return result;
}
/**
* 子節點操作
* @param fatherNode:父節點名稱
* @param fatherAttr:父節點屬性
* @param childName:要修改的.節點
* @param childValue:修改後的節點值
* @param operator: 要執行的操作名稱
*/
private synchronized String ChildOperator(String fatherNode, String fatherAttr,String childName, String childValue,String operator,Map mapAttr,int updateId) {
String result="";
if (ment == null) {
return "null";
}
Element root = ootElement();//獲取根節點名稱
if(!ame()ls(fatherNode)){ //如果不是在根節點下新增
result = XmlElementOperator(root,fatherNode,fatherAttr,childName,childValue,operator,mapAttr);
}else{
if(ls("add")){
Element childelement = lement(childName);//根節點不存在元素屬性值
ttributeValue("id",childValue);
saveXMLFile(ment);
}else if(ls("update")){
List childelements = ents(childName);
// for(Iterator childs=ator();ext();){
// Element everyone = (Element)();
// ext(childValue); //修改該元素值
// ttributeValue("id",childValue);
Element everyone = (Element)(updateId);
ttributeValue("id",childValue);
// }
saveXMLFile(ment);
}else if(ls("")){
List childelements = ents(childName);//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
List childelements1 = ents("module");
for(Iterator childs1=ator();ext();){
Element everyone1 = (Element)();
ve(everyone1);
}
}
saveXMLFile(ment);
}else if(ls("get")){
List childelements = ents(childName);//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
result = ext();
}
saveXMLFile(ment);
}else if(ls("One")){
List childelements = ents(childName);//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
String divElement = ibuteValue("id");
if(ls(childValue)){
ve(everyone);
}
}
saveXMLFile(ment);
}else if(ls("All")){
List childelements = ents();//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
List childeDiv = ents();
for(Iterator childsDiv=ator();ext();){
Element everyoneDiv = (Element)();
ve(everyoneDiv);
}
}
}
saveXMLFile(ment);
}
return result;
}
/**
* 遞迴元素操作
* @param element:要遞迴的元素
* @param fatherNode:父節點名稱
* @param fatherAttr:父節點屬性
* @param childName:要進行操作的節點
* @param childValue:操作後的節點值
* @param operator: 要執行的操作名稱
*/
private synchronized String XmlElementOperator(Element element,String fatherNode,String fatherAttr,String childName,String childValue,String operator,Map mapAttr){
String result = "";
List elements = ents();
for(Iterator it=ator();ext();){
Element currentelement = (Element)();
if(!ame()ls(fatherNode)){ //當前元素並不是我們要查詢的父元素時,繼續查詢
XmlElementOperator(currentelement,fatherNode,fatherAttr,childName,childValue,operator,mapAttr);//遞迴呼叫
}else{
if(ibuteCount()>0){ //當前元素存在屬性值時
for(Iterator list=ibuteIterator();ext();){ //遍歷屬性值
Attribute attr = (Attribute)(); //獲取屬性值佇列中的第一個元素
if(alue()ls(fatherAttr)){//根據屬性值確定惟一的父元素
if(ls("add")){//新增元素
Element childelement = lement(childName); //給當前元素新增一個子元素
ext(childValue); //設定子元素的數值
Iterator itmapAttr = et()ator();
while(ext()){
String key = (String) ();
String value = (key)ring();
ttributeValue(key,value);
}
// ttributeValue("id", "m1");
// ttributeValue("name", "module1");
// ttributeValue("url", "");
}else if(ls("update")){//修改某個元素
List childelements = ents(childName);//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
ext(childValue); //修改該元素值
}
}else if(ls("")){ //刪除某個指定的元素
List childelements = ents();//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
ve(everyone);
}
}else if(ls("get")){
List childelements = ents(childName);//獲取當前節點下的所有子節點,判斷其值,以進行修改
for(Iterator childs=ator();ext();){
Element everyone = (Element)();
//result = ext();
result =ibuteValue("id")+","+result ;
}
}
else{
//寫日誌
// String loginfo = "XmlFile Operator not exists!";
// eLogToEnd("LocalServerManager",loginfo);
}
}
}
}
}
}
saveXMLFile(ment);
return result;
}
/**
* 儲存XML檔案
* @param document: XML檔名
*/
private void saveXMLFile(Document document) {
try {
OutputFormat format = tePrettyPrint();
ncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)),format);
e(document);
e();
} catch (Exception e) {
// String loginfo = xceptionTrace(e);
// eLogToEnd("LocalServerManager", loginfo);
}
}
/**
* 判斷XML檔案是否存在.
* @param fileName
* @return
*/
private boolean fileExist() {
objFile = new (Path);
if (ts()) {
return true;
} else {
return false;
}
}
}
XML檔案:
複製程式碼 程式碼如下:
解析:這裡應用遞迴的方式來判斷是對跟節點還是子節點的操作,相對比較清晰,這裡用的是if判斷來判斷選擇的是那種操作,如果變動相對較多,我感覺可以利用依賴注入,省去了if判斷的麻煩,但是當時只是做了一個demo,沒有更多的優化,如果有興趣的話可以試一試。
總結:讀取XML其實並不難,在寫系統的時候就寫過關於xml的讀取,但是當時真的就是一個一個的寫,需要什麼在哪個方法下寫什麼,不僅要寫很多重複的程式碼,而且有一點問題需要重複的修改,所以,有時候,雖然實現了需求重要,但是怎麼實現,同樣重要!