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 |