`

Java通过反射以及Annotation将类对象写入XML文件中。

阅读更多
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;
	}
	
	
	
	
	
}





分享到:
评论

相关推荐

    JAVA_API1.6文档(中文)

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java....

    Java 1.6 API 中文 New

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio ...

    JavaAPI1.6中文chm文档 part1

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java....

    java api最新7.0

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio ...

    JavaAPI中文chm文档 part2

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java....

    [Java参考文档]

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java....

    [Java参考文档].JDK_API 1.6

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio ...

    JDK_1_6 API

    java.lang.reflect 提供类和接口,以获得关于类和对象的反射信息。 java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。 java.net 为实现网络应用程序提供类。 java.nio ...

    springmybatis

    MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...

Global site tag (gtag.js) - Google Analytics