This article describes sample code sharing for converting a java object to an xml format. if you need it, refer
Sample code for converting java objects to xml format
Package com. io; public class Person {private String name; private Integer age; private String holobby; 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 getholobby () {return holobby;} public void setholobby (String holobby) {this. holobby = holobby;} public Pe Rson (String name, Integer age, String holobby) {super (); this. name = name; this. age = age; this. holobby = holobby;} 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. document Helper; import org. dom4j. element; import org. dom4j. io. XMLWriter; public class XmlUtil {/*** use dom4j to generate an xml file * @ param object any object * @ return * @ throws SecurityException * @ throws NoSuchMethodException * @ throws InvocationTargetException * @ throws IllegalAccessException * @ throws IllegalArgumentException * @ throws IOException */public static String objectSingleDom4jToXml (Object object, string pat H) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException {org. dom4j. document document = incluenthelper. 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 Person = new Person ("test", 24, "Watching Movies and surfing the Internet"); 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 ();}}}
The above is the details shared by the sample code for converting a java object to an xml format. For more information, see other related articles in the first PHP community!