使用dom4j修改XML檔案內容

2021-08-25 06:16:08 字數 830 閱讀 5564

1、使用dom4j修改xml檔案的屬性、節點

public static int modixmlfile(string filename,string newfilename)

}/**

* 修改內容之二: 把owner項內容改為tshinghua

* 並在owner節點中加入date節點,date 節點的內容為2004-09-11,還為date 節點新增乙個屬性type

*/list = document.selectnodes("/books/owner");

it = list.iterator();

if(it.hasnext())

/** 修改內容之三: 若title內容為dom4j tutorials,則刪除該節點*/

list = document.selectnodes("/books/book");

it = list.iterator();

while(it.hasnext())

}outputformat format = outputformat.createprettyprint();

format.setencoding("gbk");

xmlwriter writer = new xmlwriter(new filewriter(new file(newfilename)),format);

writer.write(document);

writer.close();

intreturn = 1;

} catch (exception e)

return intreturn;

}

dom4j 使用dom4j生成xml

使用org.dom4j.element 建立xml 生成service.xml檔案 param tran 交易物件 param filepath 資料夾路徑 public static void exportservicexml listtranlist,string filepath servic...

使用dom4j解析xml檔案

1 使用dom4j需要引入jar包 2 直接上 獲取解析器 saxreader reader new saxreader 解析獲取document 需要引入的是dom4j下的document org.dom4j.document read reader.read src person.xml 獲取根...

使用dom4j獲取xml檔案

在idea中匯入對應jar包 成功匯入會有箭頭標誌 要獲取的xml檔案 建立相應類 public class books public books string sn,string name,double price,string author public string getsn public ...