反射+jdom+xml例子,经过一番折腾,总算知道反射是啥意思了,真的很感谢帮助我的人,不知道说啥好了,就是感动,我把我做的东西和大家分
建一个文件夹D:\some\directory
Xml类
package com.cyberobject.study;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Xml {
/**
* 向指定的xml文件写数据
* @param FileName 要用到的cell文件模板名
* @param dto 传输数据对象
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
public void writeXmlFile(DataTransaction dto) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Element rootElement = new Element( "WorkBook ");
Document myDocument = new Document(rootElement);
List dtoObjects = dto.getDtoObjects();
for(int i = 0 ; i < dtoObjects.size() ; i++){
Element cellFileElement = new Element( "CellFile ");
rootElement.addContent(cellFileElement);
DtoObject dtoObject = (DtoObject)dtoObjects.get(i);
cellFileElement.setAttribute( "name ",dtoObject.getCellFileName()+ ".cell ");
List objects = dtoObject.getObjects();
for(int j = 0 ;j < objects.size(); j++ ){
Object object = dtoObject.getObjects().get(j);
Element objElement = new Element( "Object ");
Field fieldList[] = object.getClass().getFields();
for(int k = 0 ; k < fieldList.length ; k++){
String fieldName = fieldList[k].getName().toString();
String fieldValue = fieldList[k].get(object).toString();
System.out.print(fieldName+ ": "+fieldValue);
objElement.setAttribute(fieldName,fieldValue.toString());
}
cellFileElement.addContent(objElement);
}
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
try{
File mydir=new File( "d:/some/directory/ "+dto.getXmlFileName()+ ".xml ");
if(!mydir.exists()){
this.createFile( "d:/some/directory/ ",dto.getXmlFileName());
}else{
this.deleteFile( "d:/some/directory/ "+dto.getXmlFileName()+ ".xml ");
FileWriter fileWriter = new FileWriter( "d:/some/directory/ "+dto.getXmlFileName()+ ".xml ");
output.output(myDocument,fileWriter);
fileWriter.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* 删除文件
* @param path 文件路径
*/
public void deleteFile(String path){
File f=new File(path);
try{
f.delete();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 创建文件
* @param 文件路径
* @param 文件名
* @return
* @throws IOException
*/
public boolean createFile(String _path,String _fileName)throws IOException {
boolean success = false;
String fileName = _fileName;
String filePath = _path;
try {
File file = new File(filePath);
if (!file.isDirectory()){
file.mkdirs();
}
File f = new File(filePath + File.separator + fileName + ".xml ");
success = f.createNewFile();
} catch (IOException e){
e.printStackTrace();
}
return success;
}
public static void main(String args[]) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Employee e1 = new Employee();
e1.setId( "2 ");
e1.setName( "a ");
Factory f1 = new Factory();
f1.setFactoryName( "b ");
f1.setFactoryId( "2 ");
Book b1 = new Book();
b1.setAuthor( "dudu ");
b1.setName( "titi ");
DataTransaction dto = new DataTransaction();
dto.setXmlFileName( "dd ");
DtoObject dtoObject1 = new DtoObject();
DtoObject dtoObject2 = new DtoObject();
DtoObject dtoObject3 = new DtoObject();
dtoObject1.getObjects().add(e1);
dtoObject2.getObjects().add(f1);
dtoObject3.getObjects().add(b1);
dtoObject1.setCellFileName( "aa ");
dtoObject2.setCellFileName( "bb ");
dtoObject3.setCellFileName( "cc ");
//dtoObject1.setClassName( "com.cyberobject.study.Employee ");
//dtoObject2.setClassName( "com.cyberobject.study.Factory ");
dto.getDtoObjects().add(dtoObject1);
dto.getDtoObjects().add(dtoObject2);
dto.getDtoObjects().add(dtoObject3);
Xml xml = new Xml();
xml.writeXmlFile(dto);
}
}
Employee类
package com.cyberobject.study;
public class Employee {
public String name = null;
public String id = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Factory类
package com.cyberobject.study;
public class Factory {
public String factoryName = null;
public String factoryId = null;
public String getFactoryName() {
return factoryName;
}
public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}
public String getFactoryId() {
return factoryId;
}
public void setFactoryId(String factoryId) {
this.factoryId = factoryId;
}
}
Book类
package com.cyberobject.study;
public class Book {
public String name = null;
public String author = null;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[解决办法]
帮你顶,不错啊!
[解决办法]
呵呵
[解决办法]
给大家介绍个技术群
大家一起学习,一起提高
32517633
[解决办法]
DataTransaction 这是什么东东。没贴全吧
[解决办法]
JAVA 爱好者加群 38236097
JAVA 爱好者加群 38236097
[解决办法]
呵呵
[解决办法]
UP
lcllcl987(毛爷爷) !!!