---
title: "简单的反射代码"
author: "lijing727"
date: "2010-04-28T19:34:17Z"
tags: ["XML"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-04-28T19:34:17Z"
id: 655550
url: "https://lijing727.iteye.com/blog/655550"
---

1、反射代码

```
package util;

import java.lang.reflect.Field;

import org.dom4j.Node;

public class PropertyToPojo {

	private PropertyToPojo() {

	}

	public static void setProperty(Object pojo, Node node) {

		String name = node.selectSingleNode("@name").getText();

		String value = node.selectSingleNode("/value").getText();

		try {

			Field field = pojo.getClass().getDeclaredField(name);

			// 得到域的可访问性
			boolean accessFlag = field.isAccessible();
			// 设置为可访问的
			field.setAccessible(true);
			// 设置域的值
			field.set(pojo, value);
			// 将域的可访问性设置为原来的值
			field.setAccessible(accessFlag);

		} catch (SecurityException e) {

			e.printStackTrace();

		} catch (NoSuchFieldException e) {

			e.printStackTrace();
		} catch (IllegalArgumentException e) {

			e.printStackTrace();
		} catch (IllegalAccessException e) {

			e.printStackTrace();
		}

	}

}
```

 

2、xml格式

```
	<data-source id="input1" type="csvFile">
		<property name="filePath">
			<value>D:/test.csv</value>
		</property>
	</data-source>
```