dom4j中新增節點,修改節點,刪除節點

2021-08-30 14:47:02 字數 2088 閱讀 5761

public class dom4jdemo

/*** 建立乙個xml文件,文件名由輸入引數決定

* @param filename 需建立的檔名

* @return 返回操作結果, 0表失敗, 1表成功

*/public int createxmlfile(string filename)catch(exception ex)

return returnvalue;

}/**

* 修改xml檔案中內容,並另存為乙個新檔案

* 重點掌握dom4j中如何新增節點,修改節點,刪除節點

* @param filename 修改物件檔案

* @param newfilename 修改後另存為該檔案

* @return 返回操作結果, 0表失敗, 1表成功

*/public static int modixmlfile(string filename,string newfilename,itemvo itemvo)else

} }

trycatch(exception ex)

}catch(exception ex)

return returnvalue;

}/******

* 更新xml檔案中的屬性

* @param filename

* @param newfilename

* @param itemvo

* @return

* xml檔案 ,節點位置 使用xpath比如://book[@type='society'],節點名稱, 節點新值

* @return 返回操作結果, 0表失敗, 1表成功

*/public static int updatexmlfileattribute(string filepathandname,string newfilepathandname,string notexpath,string attributename,string attributevalue)}}

/** 將document中的內容寫入檔案中 */

outputformat format = outputformat.createprettyprint();

format.setencoding( "utf-8");

xmlwriter writer = new xmlwriter(new fileoutputstream(new file(newfilepathandname)),format);

writer.write(document);

writer.close();

/** 執行成功,需返回1 */

dom4jdemo.formatxmlfile(newfilepathandname);

return 1;

} catch (documentexception e) catch (ioexception e)

return 0;

}public static int deletexmlfile(string filename,string newfilename,itemvo itemvo)

} trycatch(exception ex)

}catch(exception ex)

return returnvalue;

}public static int addxmlfile(string filename,string newfilename,itemvo itemvo)catch(exception ex)

}catch(exception ex)

return returnvalue;

}public static list loaditemlist(string filename)

}catch(exception ex)

return list;

}/**

* 格式化xml文件,並解決中文問題

* @param filename

* @return

*/public static int formatxmlfile(string filename)catch(exception ex)

return returnvalue;}}

Qt之Dom新增和修改節點

使用qtxml模組dom模式操作xml時,新增修改節點直接使用qdomelement的setnodevalue 方法不起作用,研究好久才發現使用方法有問題,正確方法如下 1 新增節點 qdomelement root doc.documentelement qdomelement newelem d...

dom4j操作節點 XPath 語法

dom4j操作節點 xpath 語法 xpath 使用路徑表示式來選取 xml 文件中的節點或節點集。節點是通過沿著路徑 path 或者步 steps 來選取的。我們將在下面的例子中使用這個 xml 文件。下面的例子使用 xml 檔案 books.xml。harry potter 29.99 lea...

JTree中修改節點

jtree tree new jtree treeselectionmodel selectionmodel 類的構造方法中 selectionmodel tree.getselectionmodel tree.seteditable true tree.getcelleditor addcelle...