java对象如何转换为xml格式

发布时间:2021-01-18 11:30:06 作者:小新
来源:亿速云 阅读:650

这篇文章主要介绍java对象如何转换为xml格式,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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格式”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. php数组转xml
  2. 如何使用JDOM将Java对象转换为XML

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java xml

上一篇:实现java版微信机器人的示例源码

下一篇:JavaScript中arguments函数有什么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》