Sample code for converting java objects to xml format

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.