java物件轉換為xml格式的範例程式碼分享

黄舟
發布: 2017-04-01 13:33:41
原創
1689 人瀏覽過

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中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板