根据XML文件,写出javabean。
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean className="jiangyi3.User">
<properties name="userName" value="jack"></properties>
<properties name="password" value="rose"></properties>
</bean>
<bean className="jiangyi3.User">
<property name="userName" value="郝键"></property>
<property name="password" value="28"></property>
</bean>
</beans>
public class User {
private String userName;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
//省略了Set和get方法
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
}
public class Person {
private String name;
preivate String age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
//省略了set和get方法
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
/* xml+dom4j+反射+BeanUtils
* 通过dom4j技术,将xml进行解析,获取数据
* 利用干涉创建对象
* 通过BeanUtils将数据导入javabean对象中*/
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo_bean {
public static void main(String[] args) throws Exception {
//创建SAXReader
SAXReader rax = new SAXReader();
//读取xml文件,返回一个Document对象
Document read = rax.read("src\\jiangyi3\\bean.xml");
//获取根元素
Element root = read.getRootElement();
//获取根元素下所有的子元素bean
List<Element> ele = root.elements();
for (Element ele1 : ele) {
//获取到每个bean标签
String className = ele1.attributeValue("className");
//根据位置创建对象,反射
Class clazz = Class.forName(className);
//创建对象
Object obj = clazz.newInstance();
//获取子标签<property>,对象中的每个属性
List<Element> ele2 = ele1.elements();
//获取每一个子标签
for (Element element : ele2) {
//获取属性名以及对应的属性值和属性名
String name = element.attributeValue("name");
String value = element.attributeValue("value");
//将属性值添加到对应的对象中
BeanUtils.setProperty(obj,name,value);
}
System.out.println(obj);
}
}
}
输出结果:User [userName=jack, password=rose]
User [userName=郝键, password=28]
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean className="jiangyi3.User">
<properties name="userName" value="jack"></properties>
<properties name="password" value="rose"></properties>
</bean>
<bean className="jiangyi3.User">
<property name="userName" value="郝键"></property>
<property name="password" value="28"></property>
</bean>
</beans>
public class User {
private String userName;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
//省略了Set和get方法
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
}
public class Person {
private String name;
preivate String age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
//省略了set和get方法
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
/* xml+dom4j+反射+BeanUtils
* 通过dom4j技术,将xml进行解析,获取数据
* 利用干涉创建对象
* 通过BeanUtils将数据导入javabean对象中*/
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo_bean {
public static void main(String[] args) throws Exception {
//创建SAXReader
SAXReader rax = new SAXReader();
//读取xml文件,返回一个Document对象
Document read = rax.read("src\\jiangyi3\\bean.xml");
//获取根元素
Element root = read.getRootElement();
//获取根元素下所有的子元素bean
List<Element> ele = root.elements();
for (Element ele1 : ele) {
//获取到每个bean标签
String className = ele1.attributeValue("className");
//根据位置创建对象,反射
Class clazz = Class.forName(className);
//创建对象
Object obj = clazz.newInstance();
//获取子标签<property>,对象中的每个属性
List<Element> ele2 = ele1.elements();
//获取每一个子标签
for (Element element : ele2) {
//获取属性名以及对应的属性值和属性名
String name = element.attributeValue("name");
String value = element.attributeValue("value");
//将属性值添加到对应的对象中
BeanUtils.setProperty(obj,name,value);
}
System.out.println(obj);
}
}
}
输出结果:User [userName=jack, password=rose]
User [userName=郝键, password=28]