這篇文章主要介紹java對(duì)象如何轉(zhuǎn)換為xml格式,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營理念,通過多達(dá)十載累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營銷推廣解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:不銹鋼雕塑等企業(yè),備受客戶贊譽(yù)。
java對(duì)象轉(zhuǎn)換為xml格式的示例代碼分享
package com.io; public class Person { private String name; private Integer age; private String hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public Person(String name, Integer age, String hobby) { super(); this.name = name; this.age = age; this.hobby = hobby; } public Person() { super(); } } package com.io; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.XMLWriter; public class XmlUtil { /** * 使用dom4j將對(duì)象生成xml文件 * @param object 任意對(duì)象 * @return * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws IOException */ public static String objectSingleDom4jToXml(Object object,String path) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException{ org.dom4j.Document document = DocumentHelper.createDocument(); Element root = document.addElement(object.getClass().getSimpleName()+"s"); Element child = root.addElement(object.getClass().getSimpleName()); Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { Method method = object.getClass().getMethod("get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1,field.getName().length())); child.addElement(field.getName()).setText(method.invoke(object)+""); } File dir = new File(path); String prefix = ".xml"; if(!dir.isDirectory()) dir.mkdirs(); File file = new File(dir+"\\"+object.getClass().getSimpleName()+prefix); if(!file.exists())file.createNewFile(); file.canExecute(); file.canRead(); file.canWrite(); Writer fileWriter = new FileWriter(file); XMLWriter xmlWriter = new XMLWriter(fileWriter); xmlWriter.write(document); xmlWriter.close(); return document.asXML(); } public static void main(String[] args) throws SecurityException, NoSuchMethodException, IOException { Person person = new Person("test", 24, "看電影、上網(wǎng)"); String str; try { str = objectSingleDom4jToXml(person,"F://create"); System.out.println(str); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
以上是“java對(duì)象如何轉(zhuǎn)換為xml格式”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!