DOM4J建立xml檔案

2021-07-26 02:47:04 字數 1234 閱讀 9176

dom4j建立xml主要會用到document和element 

其中:     addelement:新增子節點

addattribute:新增屬性

示例:public static void main(string args) throws exception {  

//建立文件的根節點  

document document = documenthelper.createdocument();  

//建立文件的 根元素節點  

element root = documenthelper.createelement("person");  

//以上兩行還可以寫成:

//element root = documenthelper.createelement("person");  

//document document = documenthelper.createdocument(root);  

document.setrootelement(root);  

//給根節點新增屬性,鍵值對形式

root.addattribute("學校", "南大").addattribute("位置", "江西");  //直接在後面可以新增多個屬性

//給根節點新增孩子節點  

element element1 = root.addelement("學生");  

//給孩子節點新增屬性

element1.addelement("姓名").addattribute("婚姻", "單身").addtext("小章");

element1.addelement("年齡").addtext("21");  

//把生成的xml文件存放在硬碟上  true代表是否換行

outputformat format = new outputformat("    ",true);

format.setencoding("gbk");//設定編碼格式  

xmlwriter xmlwriter = new xmlwriter(new fileoutputstream("d:/person.xml"),format);  

xmlwriter.write(document);  

xmlwriter.close();  

結果示例:

Dom4j 解析 建立xml

dom4j 解析xml 最近在進行介面的呼叫,接收過來是xml格式,返回格式也需要是xml格式。所以在這裡進行記錄一下!public class tests 輸出格式 msgcode name age aa cardrow list msgbody msgport saxreader reader ...

dom4j 使用dom4j生成xml

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

java通過dom4j建立xml檔案

param foldpath 各個子資料夾的xml的存放位置 param xmlpath 總xml建立後的存放位置 public static void createdescriptionxml string foldpath,string xmlpath xmlwriter new xmlwrit...