Dom4j解析XML

下面是SAX解析XML的示例,包括三个文件employee.xml、Employee.java、Dom4Employee.java

 1.被解析的employee.xml文件



	
	 
	 
	
	
	
	
	
]>

	
		tom
		20
		male
		[email protected]
		10k
	
	
		jack
		30
		female
		[email protected]
		8k
	
	
		rose
		25
		female
		[email protected]
		5k
	
	
		mark
		30
		male
		[email protected]
		5k
	

2.Employee.java文件

package exercise;

public class Employee {

	private int id;
	private String depName;
	private String name;
	private int age;
	private String gender;
	private String email;
	private String salary;
	
	public Employee() {
	}
	
	public Employee(int id, String depName, String name, int age, String gender, String email, String salary) {
		super();
		this.id = id;
		this.depName = depName;
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.email = email;
		this.salary = salary;
	}
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", depName=" + depName + ", name=" + name + ", age=" + age + ", gender=" + gender
				+ ", email=" + email + ", salary=" + salary + "]";
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDepName() {
		return depName;
	}
	public void setDepName(String depName) {
		this.depName = depName;
	}
	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 getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getSalary() {
		return salary;
	}
	public void setSalary(String salary) {
		this.salary = salary;
	}
	
	
}

3.Dom4jEmployee.java实现解析 

package exercise;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 利用Dom4j解析employee.xml文件,将其中的雇员信息添加到一个容器中
 * Dom4j的核心就在于可以使用迭代器遍历Document
 * @author Attention
 *
 */
public class Dom4jEmployee {

	private static List list = new ArrayList<>();   //创建容器用来保存employee对象
	private static Employee employee = null;                  //用来接受当前的employee
	
	public static void main(String[] args) throws Exception {
		
		//1.创建SAXReader用于读取xml
		SAXReader reader = new SAXReader();
		//2.调用read()方法读取文件,获得Document对象
		Document document = reader.read("src/exercise/employee.xml");
		//3.获取document的根元素
		Element root = document.getRootElement();
		//4.利用迭代器循环,取出每一个employee
		Iterator it = root.elementIterator();
		while(it.hasNext()) {
			employee = new Employee();
			Element element = (Element)it.next();
			String id = element.attribute("id").getStringValue();   //通过属性名获取属性值
			String depName = element.attribute("depName").getStringValue();
			employee.setId(Integer.parseInt(id));
			employee.setDepName(depName);
			
			//获得子元素的迭代器,里面包括name,age,gender,email,salary
			Iterator sit = element.elementIterator();
			while(sit.hasNext()) {
				Element se = (Element)sit.next();
				String name = se.getName();
				String value = se.getText();
				
				//将对应的值赋给相应的变量
				if(name.equals("name")) { 
					employee.setName(value);
				}else if(name.equals("age")) {
					employee.setAge(Integer.parseInt(value));
				}else if(name.equals("gender")) {
					employee.setGender(value);
				}else if(name.equals("email")) {
					employee.setEmail(value);
				}else if(name.equals("salary")) {
					employee.setSalary(value);
				}
			}
			list.add(employee);
		}
		
		for(Employee e:list) {
			System.out.println(e);
		}
	}
}

 

你可能感兴趣的:(个人总结,XML学习笔记)