建立雇员类,属性包含想下面的表格的列。 添加上题中的表格对应的 XML 数据进行解析,通过反射机制在程序中创建代表 3 行数据的 3 个雇员对象。
1、创建Employee类
package com.sxt.entity;
public class Employee {
//public String name;
private String name;
private int age;
private String job;
private int salay;
private int subsidies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalay() {
return salay;
}
public void setSalay(int salay) {
this.salay = salay;
}
public int getSubsidies() {
return subsidies;
}
public void setSubsidies(int subsidies) {
this.subsidies = subsidies;
}
public Employee(String name, int age, String job, int salay, int subsidies) {
super();
this.name = name;
this.age = age;
this.job = job;
this.salay = salay;
this.subsidies = subsidies;
}
public Employee() {
super();
}
}
2、使用employee的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="1001">
<name>高淇</name>
<age>18</age>
<jobs>程序员</jobs>
<salay>30000</salay>
<subsidies>5000</subsidies>
</employee>
<employee id="1002">
<name>高小二</name>
<age>19</age>
<jobs>讲师</jobs>
<salay>35000</salay>
<subsidies>2000</subsidies>
</employee>
<employee id="1003">
<name>高小松</name>
<age>20</age>
<jobs>教授</jobs>
<salay>10000</salay>
<subsidies>2000</subsidies>
</employee>
</employees>
3.解析xml文件(使用DOM4J方式进行)
创建SAXReader对象
调用read方式
获取根元素
通过迭代器遍历直接节点
4.创建employee对象,使用反射机制
5.封装employee对象
通过switch语句进行判断子节点
存放集合中
遍历集合
package com.sxt.test;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.omg.CORBA.INVALID_TRANSACTION;
import com.sxt.entity.Employee;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException, DocumentException {
//解析employee的xml文件
//1)创建SAXReader对象
SAXReader reader=new SAXReader();
//2)调用read方式
Document doc=reader.read(new File("employees.xml"));
//3)获取根元素
Element root=doc.getRootElement();
//4)通过迭代器遍历直接节点
for(Iterator<Element> iteEmployee=root.elementIterator();iteEmployee.hasNext();){
Element employeeEle=iteEmployee.next();
//System.out.println(employeeEle.getName());
//得到employee的属性
for(Iterator<Attribute> ateAtt= employeeEle.attributeIterator();ateAtt.hasNext();){
Attribute att=ateAtt.next();
System.out.println(att.getName()+"\t"+att.getText());
}
}
System.out.println("\n------------------");
List<Employee> empList=new ArrayList();
//通过迭代器遍历元素
for(Iterator<Element> iteEmployee=root.elementIterator();iteEmployee.hasNext();){
//创建employee对象(通过反射机制)
Class c= Class.forName("com.sxt.entity.Employee");
//1)无参构造方法
Constructor<Employee> cons=c.getConstructor();
//2)创建employee对象
Employee employee=(Employee)cons.newInstance();
Element employeeEle=iteEmployee.next();//得到每一个节点;
//使用for循环继续遍历
for(Iterator<Element> subEmployeeEle=employeeEle.elementIterator();subEmployeeEle.hasNext();){
//得到每一个子元素
Element subEle=subEmployeeEle.next();
System.out.println(subEle.getName()+"\t"+subEle.getText());
/**
* 通过反射机制封装employee对象
*/
//获取节点的名称
String nodeName=subEle.getName();//name,age,job,salay,subsidies
//使用switch判断
switch(nodeName){
case "name":
employee.setName(subEle.getText());
break;
case "age":
employee.setAge(Integer.parseInt(subEle.getText()));
break;
case "jobs":
employee.setJob(subEle.getText());
break;
case "salay":
employee.setSalay(Integer.parseInt(subEle.getText()));
break;
case "subsidies":
employee.setSubsidies(Integer.parseInt(subEle.getText()));
break;
}
}
//添加到集合中
empList.add(employee);
System.out.println("\n------------------------");
}
//遍历集合
System.out.println("\n遍历集合--------------\n");
for(Employee em:empList){
System.out.println(em.getName()+"\t"+em.getAge()+"\t"+em.getJob()+"\t"+em.getSalay()+"\t"+em.getSubsidies());
}
}
}