dom4j的使用心得

2021-07-13 20:08:36 字數 1819 閱讀 7239

一、建立document物件

1.讀取xml檔案,獲得document物件              

saxreader reader = new saxreader();

document document = reader.read(new file("csdn.xml"));

2.解析xml形式的文字,得到document物件.

string text = "";

document document = documenthelper.parsetext(text);

3.主動建立document物件.

document document = documenthelper.createdocument(); //建立根節點

element root = document.addelement("csdn");

二、配置document的doctype

// 新增doctype

defaultdocumenttype doctype = new defaultdocumenttype();

document.setdoctype(doctype);

三、document物件操作節點

// 建立文件並寫入磁碟中

document document = getmybatisdocument();

// 建立resultmap節點

resultmap.addattribute("id", mytable.getbeanname() + "map");

resultmap.addattribute("type", config.getproperty("beans") + "." + mytable.getbeanname());

// 建立insert節點

insert.addattribute("id", "insert");

insert.addattribute("parametertype", config.getproperty("beans")+"."+mytable.getbeanname());

element selectkey = insert.addelement("selectkey");

selectkey.addattribute("resulttype", "long");

selectkey.addattribute("keyproperty", mytable.getpkproperty());

selectkey.addattribute("order", "before");

selectkey.addtext("select nextval('"+mytable.getserializename()+"')");

四、寫入document到磁碟檔案中

outputformat format = outputformat.createprettyprint(); // 建立檔案輸出的時候,自動縮排的格

format.setencoding("utf-8");// 設定編碼

format.setindent(true);

format.setnewlines(true);

format.settrimtext(false);

try catch (ioexception e)

dom4j 使用dom4j生成xml

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

dom4j簡單使用

公司這邊是用dom4j,主要是用來解析xml的,一般有這麼幾個操作 1.建立xml文件 org.dom4j.document doc documenthelper.createdocument 2.建立根元素並新增 element root documenthelper.createelement ...

dom4j使用總結

1.載入xml 從檔案載入 saxreader reader new saxreader string filepath xmlfile filename xml document document null try catch documentexception e return document...