设为首页 加入收藏

TOP

简单实体类和xml文件的相互转换(二)
2017-10-12 18:09:09 】 浏览:10812
Tags:简单 实体 类和 xml 文件 相互 转换
ows
FileNotFoundException * @throws UnsupportedEncodingException */ public static <T> List<T> getEntitys(Class<T> c, String filePath, String encoding) throws UnsupportedEncodingException, FileNotFoundException { File file = new File(filePath); String labelName = c.getSimpleName().toLowerCase(); SAXReader reader = new SAXReader(); List<T> list = null; try { InputStreamReader in = new InputStreamReader(new FileInputStream(file), encoding); Document document = reader.read(in); Element root = document.getRootElement(); List elements = root.elements(labelName); list = new ArrayList<T>(); for(Iterator<Emp> it = elements.iterator(); it.hasNext();) { Element e = (Element)it.next(); T t = getEntity(c, e); list.add(t); } } catch (DocumentException e) { e.printStackTrace(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (SecurityException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (InvocationTargetException e1) { e1.printStackTrace(); } return list; } /** * 将一种类型 和对应的 xml元素节点传进来,返回该类型的对象,该方法不对外开放 * @param c 类类型 * @param ele 元素节点 * @return 该类型的对象 * @throws InstantiationException * @throws IllegalAccessException * @throws NoSuchMethodException * @throws SecurityException * @throws IllegalArgumentException * @throws InvocationTargetException */ @SuppressWarnings("unchecked") private static <T> T getEntity(Class<T> c, Element ele) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { Field[] fields = c.getDeclaredFields(); Object object = c.newInstance();// for(Field f : fields) { String type = f.getType().toString();//获得字段的类型 String fieldName = f.getName();//获得字段名称 String param = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);//把字段的第一个字母变成大写 Element e = ele.element(fieldName); if(type.indexOf("Integer") > -1) {//说明该字段是Integer类型 Integer i = null; if(e.getTextTrim() != null && !e.getTextTrim().equals("")) { i = Integer.parseInt(e.getTextTrim()); } Method m = c.getMethod("set" + param, Integer.class); m.invoke(object, i);//通过反射给该字段set值 } if(type.indexOf("Double") > -1) { //说明该字段是Double类型 Double d = null; if(e.getTextTrim() != null && !e.getTextTrim().equals("")) { d = Double.parseDouble(e.getTextTrim()); } Method m = c.getMethod("set" + param, Double.class); m.invoke(object, d); } if(type.indexOf("String") > -1) {//说明该字段是String类型 String s = null; if(e.getTextTrim() != null && !e.getTextTrim().equals("")) { s = e.getTextTrim(); } Method m = c.getMethod("set" + param, String.class); m.invoke(object, s); } } return (T)object; } /** * 用来写xml文件 * @param doc Document对象 * @param filePath 生成的文件路径 * @param encoding 写xml文件的编码 */ public static void writeXml(Document doc, String filePath, String encoding) { XMLWriter writer = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(encoding);// 指定XML编码&n
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SpringMVC---进阶篇 下一篇关于Date 时区问题闹得一个小笑话

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目