dom4j生成、解析xml
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]深夜无人的街道
- 下一篇: 洛谷P1265 公路修建题解