首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

JAVA泛型实业集合转xml

2012-08-29 
JAVA泛型实体集合转xml .public static String beanListToXml(List? list ,Class? cls) throws Except

JAVA泛型实体集合转xml .

public static String beanListToXml(List<?> list ,Class<?> cls) throws Exception{return beanListToXml(list, cls,"yyyy-MM-dd");}public static String beanListToXml(List<?> list ,Class<?> cls,String pattern) throws Exception{StringBuffer sb = new StringBuffer();sb.append("<?xml version="1.0" encoding="utf-8"?>").append("\r\n");String className = cls.getSimpleName();sb.append("<").append(className).append("List>").append("\r\n");Field[] fileds = cls.getDeclaredFields();fileds = getFileds(cls,fileds);try {for(int i=0;i<list.size();i++){sb.append("<").append(className).append(">").append("\r\n");for(int j = 0;j<fileds.length;j++){String filedName = fileds[j].getName();String getMethodName = "get"+ filedName.substring(0, 1).toUpperCase()+ filedName.substring(1);Method getMethod = cls.getMethod(getMethodName,new Class[] {});Object value = getMethod.invoke(list.get(i), new Object[] {});String textValue = "";if (value instanceof Date) {SimpleDateFormat sdf = new SimpleDateFormat(pattern);textValue = sdf.format((Date) value);}else{textValue = Tool.nvl(value);}sb.append("<").append(filedName).append(">");sb.append(chargeStr(textValue));sb.append("</").append(filedName).append(">");sb.append("\r\n");}sb.append("</").append(className).append(">").append("\r\n");}sb.append("</").append(className).append("List>").append("\r\n");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();throw new DataException("实体转换成xml时出错");}return sb.toString();}private static Field[] getFileds(Class<?> cls,Field[] fileds) {// TODO Auto-generated method stubField[] result = null;Class parent = cls.getSuperclass();if(parent != null){Field[] pfields = parent.getDeclaredFields();result = new Field[fileds.length+pfields.length];System.arraycopy(fileds, 0, result, 0, fileds.length);for(int i=0;i<pfields.length;i++){result[fileds.length+i] = pfields[i];}getFileds(parent, fileds);}return result;}private static String chargeStr(String str){String result = str;result = result.replace("<", "<");result = result.replace(">", ">");result = result.replace("&", "&");result = result.replace("'", "'");result = result.replace(""", """);return result;}

?

热点排行