一、实操名称:建立Employee类,属性包含上面表格的列。 并将上题中的表格对应的XML数据进行解析,通过反射机制在程序中创建代表3行数据的3个Employee对象。
二、项目信息
2.1作者:任鑫
2.2项目时长:15分钟
2.3项目环境:JDK版本--1.8,操作系统--Windows7,eclipse
2.4项目涉及知识点:XML技术、Schema技术、DOM4J技术、集合、switch、简单类、反射
2.5项目的搭建
2.5.1--项目简介
1、使用XML表达表格数据:编写employee.xml;
2、验证:编写employee.xsd;
3、测试验证成功与否:Test类;
4、数据封装:Employee类;
5、解析XML文件并用反射机制创建Employee类对象:TestDOM4JReflection类。
2.5.2--employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<employees xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:noNamespaceSchemaLocation="{employee.xsd}">
<employee id="1001">
<id>1001</id>
<name>高淇</name>
<age>18</age>
<post>程序员</post>
<salary>30000</salary>
<allowance>5000</allowance>
</employee>
<employee id="1002">
<id>1002</id>
<name>高小二</name>
<age>19</age>
<post>讲师</post>
<salary>35000</salary>
<allowance>2000</allowance>
</employee>
<employee id="1003">
<id>1003</id>
<name>高小松</name>
<age>20</age>
<post>教授</post>
<salary>20000</salary>
<allowance>3000</allowance>
</employee>
</employees>
2.5.3--employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int"></xs:element>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="age" type="xs:int"></xs:element>
<xs:element name="post" type="xs:string"></xs:element>
<xs:element name="salary" type="xs:double"></xs:element>
<xs:element name="allowance" type="xs:double"></xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
2.5.4--Test类
package cn.sxt.schema;
import java.io.File;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
/**
* 利用Schema技术验证编写的employee.xml文件是否有效
* @author littlelock
*
*/
public class Test {
public static void main(String[] args) throws SAXException {
//(1)创建SchemaFactory工厂
SchemaFactory sch = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
//(2)建立验证文件对象
File schemaFile = new File("employee.xsd");
//(3)利用SchemaFactory工厂对象,接收验证的文件对象,生成Schema对象
Schema schema = sch.newSchema(schemaFile);
//(4)产生对此schema的验证器
Validator validator = schema.newValidator();
//(5)要验证的数据(准备数据源)
Source source = new StreamSource("employee.xml");
//(6)开始验证
try {
validator.validate(source);
System.out.println("验证成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("验证失败");
}
}
}
2.5.5--Employee类
package cn.sxt.entity;
/**
* 将表格数据封装在类对象中
* @author littlelock
*
*/
public class Employee {
//私有属性
private int id;
private String name;
private int age;
private String post;
private double salary;
private double allowance;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getAllowance() {
return allowance;
}
public void setAllowance(double allowance) {
this.allowance = allowance;
}
public Employee(int id, String name, int age, String post, double salary,
double allowance) {
super();
this.id = id;
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.allowance = allowance;
}
public Employee() {
super();
}
@Override
public String toString() {
return id+"t"+ name +"t" + age+"t" + post +"t"+ salary +"t"+ allowance ;
}
}
2.5.6--TestDOM4JReflection类
package cn.sxt.dom4jreflection;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import cn.sxt.entity.Employee;
/**
* 使用DOM4J解析XML数据,然后通过反射创建Employee的对象
* @author littlelock
*
*/
public class TestDOM4JReflection {
public static void main(String[] args) throws DocumentException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//(1)创建SAXReader读对象
SAXReader reader = new SAXReader();
//(2)调用read方法
Document doc = reader.read(new File("employee.xml"));
//(3)获取根元素
Element root = doc.getRootElement();//employees
//(4)节点中的信息封装到Employee对象中,并储存在List集合中,最后输出
List<Employee> employeeList = new ArrayList<Employee>();//集合存储Employee对象
for(Iterator<Element> iteEmployee = root.elementIterator();iteEmployee.hasNext();){
Employee employee = new Employee();//创建Employee对象
Element employeeEle = iteEmployee.next();//得到每一个employee节点
for(Iterator<Element> subEmployeeEle = employeeEle.elementIterator();subEmployeeEle.hasNext();){
//得到每一个子元素
Element subEle = subEmployeeEle.next();
/**
* 封装成Employee对象
*/
//获取节点的名称
String nodeName = subEle.getName();
//使用switch判断
switch(nodeName){
case "id":
employee.setId(Integer.parseInt(subEle.getText()));
break;
case "name":
employee.setName(subEle.getText());
break;
case "age":
employee.setAge(Integer.parseInt(subEle.getText()));
break;
case "post":
employee.setPost(subEle.getText());
break;
case "salary":
employee.setSalary(Double.parseDouble(subEle.getText()));
break;
case "allowance":
employee.setAllowance(Double.parseDouble(subEle.getText()));
break;
}
}
employeeList.add(employee);
}
System.out.println("遍历集合-----------------");
for(Employee e:employeeList){
System.out.println(e.getId()+"t"+e.getName()+"t"+e.getAge()+"t"+e.getPost()+"t"+e.getSalary()+"t"+e.getAllowance());
}
System.out.println("===========================");
//通过反射创建Employee对象
Class c = Class.forName("cn.sxt.entity.Employee");//获取Class类对象
Constructor cons =c.getConstructor(int.class,String.class,int.class,String.class,double.class,double.class); //得到构造方法的对象
Employee employee1 = (Employee) cons.newInstance(1001,"高淇",18,"程序员",30000,5000); //通过构造方法的对象,创建Employee类的对象
Employee employee2 = (Employee) cons.newInstance(1002,"高小二",19,"讲师",35000,2000); //通过构造方法的对象,创建Employee类的对象
Employee employee3 = (Employee) cons.newInstance(1003,"高小松",20,"教授",20000,3000); //通过构造方法的对象,创建Employee类的对象
Employee[] employee = {employee1,employee2,employee3};
for(Employee e:employee){
System.out.println(e.toString());
}
}
}
2.5.7--运行结果: