DOM解析XML

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

1.被解析的employee.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE employees [
	<!ELEMENT employees (employee*)>
	<!ELEMENT employee (name,age,gender,email,salary)>
	<!ATTLIST employee id  CDATA #IMPLIED> 
	<!ATTLIST employee depName CDATA #IMPLIED> 
	<!ELEMENT name (#PCDATA)>
	<!ELEMENT age (#PCDATA)>
	<!ELEMENT gender (#PCDATA)>
	<!ELEMENT email (#PCDATA)>
	<!ELEMENT salary (#PCDATA)>
]>
<employees>
	<employee id="1" depName="教学部">
		<name>tom</name>
		<age>20</age>
		<gender>male</gender>
		<email>tom@163.com</email>
		<salary>10k</salary>
	</employee>
	<employee id="2" depName="教学部">
		<name>jack</name>
		<age>30</age>
		<gender>female</gender>
		<email>jack@163.com</email>
		<salary>8k</salary>
	</employee>
	<employee id="3" depName="市场部">
		<name>rose</name>
		<age>25</age>
		<gender>female</gender>
		<email>rose@163.com</email>
		<salary>5k</salary>
	</employee>
	<employee id="4" depName="教学管理部">
		<name>mark</name>
		<age>30</age>
		<gender>male</gender>
		<email>mark@163.com</email>
		<salary>5k</salary>
	</employee>
</employees>

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.DomEmployee.java实现解析

package exercise;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 我们利用Dom解析读取employee.xml文件中的员工信息,将其存放到容器中
 * 同时我们加入封装的思想
 * @author www11
 *
 */
public class DomEmployee {

	private  List<Employee> list;  //创建一个容器用于保存Employee对象
	private  Employee employee;    //接受每一个employee
	public DomEmployee() {        //构造器
		list = new ArrayList<Employee>();
	}	
	public List<Employee> getList() { //提供getList()方法,从外部获取
		return list;
	}
	//获取文档树类型对象的函数,大家不要被try..catch语句给迷惑,代码很简单
	private Document getDocument(File file) {
		//1.创建Dom解析工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document document = null;
		//2.用工厂生成一个Dom构建器
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			try {
				//3.用Dom构建器去调用其中的解析方法,方法返回一个文档树类型的对象,我们只要对这个文档树对象操作就可以得到想要的结果了
				document = builder.parse(file);
			}catch (Exception e) {
				e.printStackTrace();
			}	
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	//将获取到的文档解析出来的结果添加到list中
	private void parser(Document document) {
		//直接通过标签名获得节点集合
		NodeList nodeList = document.getElementsByTagName("employee");
		
		for(int i = 0; i < nodeList.getLength();i++) {
			//将每一个节点转换为元素类型
			Element e = (Element)nodeList.item(i);
			employee = new Employee();
			String id = e.getAttribute("id");       //通过属性名直接获取属性值
			String depName = e.getAttribute("depName");
			employee.setId(Integer.parseInt(id));   //将获取到的值赋给employee
			employee.setDepName(depName);
			
			NodeList nodeList2 = e.getChildNodes();  //获取到子节点集合,其中包括name,age,gender,email,salary
			for(int j = 0;j<nodeList2.getLength();j++) {
				if(nodeList2.item(j).getNodeType() == Node.ELEMENT_NODE) {  //这个判断用于去掉空文本
					
					String name = nodeList2.item(j).getNodeName();   //获取到当前节点的名字
					String value = nodeList2.item(j).getTextContent();  //获取到当前节点的文本内容
					
					//将对应的值赋给相应的变量
					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);    //将本次读取到的employee加入到容器中
		}
	}
	
	//主函数测试
	public static void main(String[] args) throws Exception {	
		DomEmployee dom = new DomEmployee();
		File file = new File("src/exercise/employee.xml");
		Document document = dom.getDocument(file);
		dom.parser(document);
		
		List<Employee> list = dom.getList();
		for(Employee e : list) {
			System.out.println(e);
		}
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值