java对象转换为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将对象生成xml文件 
* @param object 任意对象 
* @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, "看电影、上网"); 
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对象转换为xml格式的示例代码分享的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:java,xml,对象
  • 程序员必备接口测试调试工具:点击使用

    Apipost = Postman + Swagger + Mock + Jmeter

    Api设计、调试、文档、自动化测试工具

    网页生成APP,用做网站的技术去做APP:立即创建

    手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包

    • 上一篇:XML数据岛之数据修改与添加
    • 下一篇:maven项目不编译xml文件的解决办法

    相关文章

    相关视频


    • 使用xmlhttp为网站增加域名查询功能的示例代码...
    • 四种XML解析方式详解
    • 基于PHP对XML的操作详解
    • XML和Tomcat的入门知识的详细介绍
    • java对象转换为xml格式的示例代码分享
    • Vue3 事件修饰符
    • vue3 指令
    • vue3 基础语法
    • vue3 组合api和选项api介绍

    视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • Laravel视频教程
    • Vue视频教程

    专题

    java对象转换为xml格式的示例代码分享