![9fbd61c17e51c5bd0eb6cc49bbb6d7d6.png](https://i-blog.csdnimg.cn/blog_migrate/60b095834e3f20bd524924b563a47710.jpeg)
![47c43f3deba9c426e05c0f1d2dbac3db.png](https://i-blog.csdnimg.cn/blog_migrate/59db4ef3661023381155003008fd412e.png)
建立Employee类,属性包含上面表格的列。并将上题中的表格对应的XML数据进行解析,通过反射机制在程序中创建代表3行数据的3个Employee对象
1、创建一个Employee类:
package com.chenpeng.Employee;
public class Employee {
private int id;
private String name;
private int age;
private String job;
private double salary;
private double subsidy;
public Employee() {
super();
}
public Employee(int id, String name, int age, String job, double salary, double subsidy) {
super();
this.id = id;
this.name = name;
this.age = age;
this.job = job;
this.salary = salary;
this.subsidy = subsidy;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSubsidy() {
return subsidy;
}
public void setSubsidy(double subsidy) {
this.subsidy = subsidy;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", job=" + job + ", salary=" + salary
+ ", subsidy=" + subsidy + "]";
}
}
使用DOM4J解析XML文档
<?xml version="1.0" encoding="UTF-8"?>
<emps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="{Employee.xsd}">
<emp id="1001">
<name>高淇</name>
<age>18</age>
<job>程序员</job>
<salary>30000</salary>
<subsidy>5000</subsidy>
</emp>
<emp id="1002">
<name>高小二</name>
<age>19</age>
<job>讲师</job>
<salary>35000</salary>
<subsidy>2000</subsidy>
</emp>
<emp id="1003">
<name>高小松</name>
<age>20</age>
<job>教授</job>
<salary>20000</salary>
<subsidy>3000</subsidy>
</emp>
</emps>
schema约束
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="emps">
<xs:complexType>
<xs:sequence>
<xs:element name="emp" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="age" type="xs:int"></xs:element>
<xs:element name="job" type="xs:string"></xs:element>
<xs:element name="salary" type="xs:double"></xs:element>
<xs:element name="subsidy" 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>
测试类:
![7deea94551b798c4532d18446d799303.png](https://i-blog.csdnimg.cn/blog_migrate/f8331bce3f565b7b24cb9dcca811788c.jpeg)
![b8b825bae7dd69dd2c43026e98788da9.png](https://i-blog.csdnimg.cn/blog_migrate/fb95c32d5b826c179b17a5d92d9c2d40.jpeg)
![35edff52a5f49ca7a569a5b1140a0d16.png](https://i-blog.csdnimg.cn/blog_migrate/9e43cc66dcd823a69e03262b40b5907d.png)
![3536885a1d551dc2e679c95a9420a81a.png](https://i-blog.csdnimg.cn/blog_migrate/c247882b4a0dc9237eb3b9fd46cd7b6f.jpeg)
![f3341984a605885539d624d011222f61.png](https://i-blog.csdnimg.cn/blog_migrate/d89483dd167e133becba8b9fdb748c4b.png)