Java透过反射以及Annotation将类对象写入XML文件中
Java通过反射以及Annotation将类对象写入XML文件中。
这里用到了反射以及注释。
注释的作用是将英文的对象名称转换成中文, 存储在xml文件中。
比如我在Person类中有一个password,但是我想在xml文件中以‘密码’作为节点名称。
那么就通过注释来获取。
在Person类中的代码为:
package com.lj.test; import java.io.File; import java.io.FileWriter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import com.lj.annotation.NodeAnnotation; import com.lj.model.Person; public class WriteRef { public static void main(String[] args) throws Exception { Person p1 = new Person("li", "233", 25); Person p2 = new Person("wu", "123", 27); List<Person> al = new ArrayList<Person>(); al.add(p1); al.add(p2); Document doc = DocumentHelper.createDocument(); doc = writeList2XML(doc, al); XMLWriter writer=new XMLWriter(new FileWriter(new File("src/main/resources/Person.xml")),OutputFormat.createPrettyPrint()); writer.write(doc); writer.flush(); writer.close(); } @SuppressWarnings({ "rawtypes", "unchecked" }) private static Document writeList2XML(Document doc, List<?> objs) throws Exception { String temp_name = objs.get(0).getClass().getSimpleName(); String firstLetter = temp_name.substring(0, 1).toLowerCase(); String objName = firstLetter + temp_name.substring(1, temp_name.length()); String rootName = objName + "s"; // xml文件的跟目录名称 System.out.println("objname= " + objName); Element root = doc.addElement(rootName); Class clz = objs.get(0).getClass(); Field[] fields = clz.getDeclaredFields(); for (Object obj : objs) { Element ele = root.addElement(objName); for (Field f : fields) { //类属性名称, 用于获取getter和setter String name = f.getName(); String firstLetter_ = name.substring(0, 1).toUpperCase(); System.out.println(name); String getMethodName = "get" + firstLetter_ + name.substring(1, name.length()); Method getMethod = clz.getMethod(getMethodName, new Class[] {}); Object valueObj = getMethod.invoke(obj, new Class[] {}); String value = valueObj.toString(); System.out.println("value= " + value); //检查get方法是否包含注释 if (getMethod.isAnnotationPresent(NodeAnnotation.class)) { String nodeName = getMethod.getAnnotation( NodeAnnotation.class).nodeName(); System.out.println("nodeName= -----------" + nodeName); ele.addElement(nodeName).addText(value); } else { ele.addElement(name).addText(value); } if (name.equals("id")) { System.out.println("***********id=" + value + "********"); ele.addAttribute(name, value); } } } return doc; } }
这里用到了反射以及注释。
注释的作用是将英文的对象名称转换成中文, 存储在xml文件中。
比如我在Person类中有一个password,但是我想在xml文件中以‘密码’作为节点名称。
那么就通过注释来获取。
在Person类中的代码为:
package com.lj.model; import com.lj.annotation.NodeAnnotation; public class Person { private String name; private String password; private int age; @NodeAnnotation(nodeName = "名字") public String getName() { return name; } public void setName(String name) { this.name = name; } @NodeAnnotation(nodeName = "密码") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, String password, int age) { super(); this.name = name; this.password = password; this.age = age; } }