import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.fh.hibernate.pojo.TblUserId;
public class XmlHelper {
public static String toXml(List list) {
StringBuffer bf = new StringBuffer();
bf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<objects>\n");
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
bf.append("\t<" + obj.getClass().getSimpleName()+">\n");
Field[] fields = obj.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
try {
String name = fields[j].getName();
Method method = obj.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1), new Class[] {});
Object result = method.invoke(obj, new Object[] {});
bf.append("\t\t<" + name + ">" + (result==null?"":result)+"</"+name+">\n");
} catch (Exception e) {
}
}
bf.append("\t</"+ obj.getClass().getSimpleName()+">\n");
}
bf.append("</objects>\n");
return bf.toString();
}
public static void main(String[] args) {
List<TblUserId> list = new ArrayList<TblUserId>();
TblUserId tblUserId = new TblUserId();
tblUserId.setOrganizationId("i1");
tblUserId.setUserId("n1");
TblUserId user2 = new TblUserId();
user2.setOrganizationId("i2");
// user2.setUserId("n2");
list.add(tblUserId);
list.add(user2);
XmlHelper c = new XmlHelper();
System.out.println(c.toXml(list));
}
}
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.fh.hibernate.pojo.TblUserId;
public class XmlHelper {
public static String toXml(List list) {
StringBuffer bf = new StringBuffer();
bf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<objects>\n");
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
bf.append("\t<" + obj.getClass().getSimpleName()+">\n");
Field[] fields = obj.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
try {
String name = fields[j].getName();
Method method = obj.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1), new Class[] {});
Object result = method.invoke(obj, new Object[] {});
bf.append("\t\t<" + name + ">" + (result==null?"":result)+"</"+name+">\n");
} catch (Exception e) {
}
}
bf.append("\t</"+ obj.getClass().getSimpleName()+">\n");
}
bf.append("</objects>\n");
return bf.toString();
}
public static void main(String[] args) {
List<TblUserId> list = new ArrayList<TblUserId>();
TblUserId tblUserId = new TblUserId();
tblUserId.setOrganizationId("i1");
tblUserId.setUserId("n1");
TblUserId user2 = new TblUserId();
user2.setOrganizationId("i2");
// user2.setUserId("n2");
list.add(tblUserId);
list.add(user2);
XmlHelper c = new XmlHelper();
System.out.println(c.toXml(list));
}
}