日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dom4j生成、解析xml

發布時間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dom4j生成、解析xml 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java代碼 ?
  • /**?? ?
  • ????*?創建xml?? ?
  • ????*?@param?obj????????泛型對象?? ?
  • ????*?@param?entityPropertys?泛型對象的List集合?? ?
  • ????*?@param?Encode?????XML自定義編碼類型? ?
  • ????*?@param?XMLPath????XML文件的路徑及文件名?? ?
  • ????*?@date?2011-5-8 ?
  • ????*?@author?lujan ?
  • ????*/?? ??
  • ???public?void?createXmlDocument(T?obj,?List<T>?entityPropertys,?String?Encode,??? ??
  • ???????????String?XMLPath)?{??? ??
  • ???????try?{??? ??
  • ???????????XMLWriter?writer?=?null;//?聲明寫XML的對象???? ??
  • ???????????OutputFormat?format?=?OutputFormat.createPrettyPrint();??? ??
  • ???????????format.setEncoding(Encode);??//?設置XML文件的編碼格式?UTF-8?? ??
  • ??
  • ???????????File?file?=?new?File(XMLPath);//獲得文件????? ??
  • ? ??
  • ???????????if?(file.exists())?{??? ??
  • ???????????????file.delete();??? ??
  • ???????????}??? ??
  • ???????????//?創建xml文件?? ??
  • ???????????Document?document?=?DocumentHelper.createDocument();??? ??
  • ???????????String?rootname?=?obj.getClass().getSimpleName();??//獲得簡單類名??? ??
  • ???????????Element?root?=?document.addElement(rootname?+?"s");//添加根節點??? ??
  • ???????????Field[]?properties?=?obj.getClass().getDeclaredFields();//獲得實體類的所有屬性??? ??
  • ???????????//賦值 ??
  • ???????????for?(T??t?:?entityPropertys)?{????????????????????????????????//遞歸實體??? ??
  • ???????????????Element?element?=?root.addElement(rootname);????????????//二級節點??? ??
  • ???????????????for?(int?i?=?0;?i?<?properties.length;?i++)?{?????????????????????? ??
  • ???????????????????//反射get方法??????? ??
  • ???????????????????Method?meth?=?t.getClass().getMethod("get"+?properties[i].getName().substring(0,?1).toUpperCase()?+?properties[i].getName().substring(1));??? ??
  • ???????????????????//為二級節點添加屬性,屬性值為對應屬性的值??? ??
  • ???????????????????element.addElement(properties[i].getName()).setText(meth.invoke(t).toString());??? ??
  • ???????????????}??? ??
  • ???????????}??? ??
  • ???????????writer?=?new?XMLWriter(new?FileOutputStream(file),?format); ??
  • ???????????writer.write(document);??? ??
  • ???????????writer.close();??? ??
  • ???????}?catch?(Exception?e)?{??? ??
  • ???????????System.out.println("XML文件寫入失敗");? ??
  • ???????????e.printStackTrace(); ??
  • ???????}??? ??
  • ???}????
  • ? Java代碼 ?
  • /**?? ?
  • ??*???解析 ?
  • ??*?@param?XMLPath?XML文件的路徑(含名字) ?
  • ??*?@param?t?????泛型對象?? ?
  • ??*?@return??List<T> ?
  • ??*?@date?2011-5-8 ?
  • ??*?@author?lujn ?
  • ??*/???? ??
  • ?public?List<T>?parserXml(String?XMLPath,?T?t)?{??? ??
  • ?????List<T>?list?=?new?ArrayList<T>();???//創建list集合??? ??
  • ?????try?{??? ??
  • ?????????File?f?=?new?File(XMLPath);????//讀取文件?????????? ??
  • ?????????SAXReader?reader?=?new?SAXReader();?? ??
  • ?????????Document?doc?=?reader.read(f);?????//dom4j讀取?? ??
  • ?????????Element?root?=?doc.getRootElement();//獲得根節點??? ??
  • ?????????Element?secondElement;?????????????//二級節點??? ??
  • ?????????Field[]?properties?=?t.getClass().getDeclaredFields();//獲得實例的屬性????? ??
  • ??
  • ?????????Method?setMethod?=?null;??? ??
  • ?????????for?(Iterator?i?=?root.elementIterator(t.getClass().getSimpleName());?i.hasNext();)?{ ??
  • ????????????//遍歷t.getClass().getSimpleName()節點??? ??
  • ????????????secondElement?=?(Element)?i.next();?????????//下一個二級節點??? ??
  • ????????????t?=?(T)t.getClass().newInstance();??????????//獲得對象的新的實例??? ??
  • ????????????Object[]?args?=?new?Object[properties.length]; ??
  • ????????????for?(int?j?=?0;?j?<?properties.length;?j++)?{ ??
  • ???????????????//遍歷所有孫子節點??? ??
  • ????????????????//實例的set方法??? ??
  • ???????????????setMethod?=?t.getClass().getMethod("set"+?properties[j].getName().substring(0,?1).toUpperCase()+?properties[j].getName().substring(1),properties[j].getType()); ??
  • ???????????????setMethod.invoke(t,secondElement.elementText(properties[j].getName()))?; ??
  • ?????????????}??? ??
  • ?????????????list.add(t);??? ??
  • ?????????}????????????? ??
  • ?????}?catch?(Exception?e)?{??? ??
  • ?????????e.printStackTrace();??? ??
  • ?????}??? ??
  • ?????return?list;??? ??
  • ?}?????? ??
  • ???
  • 轉載于:https://www.cnblogs.com/JohnLiang/archive/2011/12/23/2298897.html

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的dom4j生成、解析xml的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。