JAVA对象和XML文件绑定

目的

    在日常工作中,我们经常处理这些编组关系:JAVA对象 <--> XML文件;JAVA对象 <--> 数据库表。本文简单介绍一下JAVA对象和XML文件之间的绑定。虽然代码简单,但解决问题的广度与思路也是非常重要的。


    首先,在Maven中引入要用到的jar包。

		<dependency>
			<groupId>castor</groupId>
			<artifactId>castor</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xerces</artifactId>
			<version>2.4.0</version>
		</dependency>


    1. 写两个有包含关系的Bean

package shuai.study.castor.bean;

import java.util.Collection;
import java.util.LinkedList;

public class CompanyBean {
	private String type;
	private String name;
	private String address;
	private Collection<EmployeeBean> employeeBeanCollection;

	public CompanyBean() {
		this.employeeBeanCollection = new LinkedList<EmployeeBean>();
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Collection<EmployeeBean> getEmployeeBeanCollection() {
		return this.employeeBeanCollection;
	}

	public void setEmployeeBeanCollection(Collection<EmployeeBean> employeeBeanCollection) {
		this.employeeBeanCollection = employeeBeanCollection;
	}

	public void addEmployeeBean(EmployeeBean employeeBean) {
		this.employeeBeanCollection.add(employeeBean);
	}
}

package shuai.study.castor.bean;

public class EmployeeBean {
	private String id;
	private String name;
	private String sex;
	private int age;
	private String address;

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return this.sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return this.age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

    2. 我们定义一个mapping文件,阐述JAVA对象和XML文件之间的关系。

<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
	<description>Flight Route Mapping</description>
	<class name="shuai.study.castor.bean.CompanyBean">
		<map-to xml="company" />
		<field name="type">
			<bind-xml name="type" node="attribute" />
		</field>
		<field name="name">
			<bind-xml name="name" />
		</field>
		<field name="address">
			<bind-xml name="address" />
		</field>
		<field name="employeeBeanCollection" collection="collection"
			type="shuai.study.castor.bean.EmployeeBean">
			<bind-xml name="employee" />
		</field>
	</class>
	<class name="shuai.study.castor.bean.EmployeeBean" auto-complete="true">
		<field name="id">
			<bind-xml name="id" node="attribute" />
		</field>
		<field name="name">
			<bind-xml name="name" />
		</field>
		<field name="sex">
			<bind-xml name="sex" />
		</field>
		<field name="age">
			<bind-xml name="age" />
		</field>
		<field name="address">
			<bind-xml name="address" />
		</field>
	</class>
</mapping>

    3. 写一个测试类,处理JAVA对象与XML文件之间的编组与反编组。

package shuai.study.castor;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

import shuai.study.castor.bean.CompanyBean;
import shuai.study.castor.bean.EmployeeBean;

public class CastorDemo {

	public static void main(String[] args) {
		EmployeeBean employeeBean = new EmployeeBean();
		employeeBean.setId("37");
		employeeBean.setName("shuai");
		employeeBean.setSex("male");
		employeeBean.setAge(30);
		employeeBean.setAddress("Ronghua South Road, Hi-Tech Zone, Chengdu");

		CompanyBean companyBean = new CompanyBean();
		companyBean.setType("foreign-owned");
		companyBean.setName("Tieto");
		companyBean.setAddress("Tianfu Software Park, Tianfu Avenue, Hi-Tech Zone, Chengdu");
		companyBean.addEmployeeBean(employeeBean);

		try {
			// Mapping file path
			String companyEmployeeMapPath = CastorDemo.class.getResource("/file/map/company_employee_map.xml").getPath();
			Mapping companyEmployeeMapping = new Mapping();
			// Loading Mapping file
			companyEmployeeMapping.loadMapping(companyEmployeeMapPath);

			// Output file path, make sure this path exist
			String companyEmployeeOutputPath = CastorDemo.class.getResource("/file/output/company_employee_output.xml").getPath();
			File companyEmployeeOutputFile = new File(companyEmployeeOutputPath);

			Writer companyEmployeeWriter = new FileWriter(companyEmployeeOutputFile);

			// Marshal Object to XML
			Marshaller marshaller = new Marshaller(companyEmployeeWriter);
			marshaller.setMapping(companyEmployeeMapping);
			marshaller.marshal(companyBean);

			// Un-Marshal XML to Object
			Reader companyEmployeeReader = new FileReader(companyEmployeeOutputFile);
			Unmarshaller unmarshaller = new Unmarshaller(companyEmployeeMapping);
			CompanyBean companyEmployeeBean = (CompanyBean) unmarshaller.unmarshal(companyEmployeeReader);

			System.out.println("Company is " + companyEmployeeBean.getName() + " at " + companyEmployeeBean.getAddress());
		} catch (IOException ioe) {
			ioe.printStackTrace(System.err);
		} catch (MarshalException me) {
			me.printStackTrace(System.err);
		} catch (ValidationException ve) {
			ve.printStackTrace(System.err);
		} catch (MappingException me) {
			me.printStackTrace(System.err);
		}
	}
}


上一篇:性能测试工具操作数据库(二)-Loadrunner与oracle


下一篇:用老项目来缅怀下逝去的青春