JAVA对象和XML文件绑定

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

目的

    在日常工作中,我们经常处理这些编组关系: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);
		}
	}
}


相关文章
|
17天前
|
安全 Java 编译器
Java对象一定分配在堆上吗?
本文探讨了Java对象的内存分配问题,重点介绍了JVM的逃逸分析技术及其优化策略。逃逸分析能判断对象是否会在作用域外被访问,从而决定对象是否需要分配到堆上。文章详细讲解了栈上分配、标量替换和同步消除三种优化策略,并通过示例代码说明了这些技术的应用场景。
Java对象一定分配在堆上吗?
|
21天前
|
Java API
Java 对象释放与 finalize 方法
关于 Java 对象释放的疑惑解答,以及 finalize 方法的相关知识。
42 17
|
20天前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第22天】在Java的世界里,对象序列化和反序列化是数据持久化和网络传输的关键技术。本文将带你了解如何在Java中实现对象的序列化与反序列化,并探讨其背后的原理。通过实际代码示例,我们将一步步展示如何将复杂数据结构转换为字节流,以及如何将这些字节流还原为Java对象。文章还将讨论在使用序列化时应注意的安全性问题,以确保你的应用程序既高效又安全。
|
30天前
|
存储 Java 数据管理
Java零基础-Java对象详解
【10月更文挑战第7天】Java零基础教学篇,手把手实践教学!
25 6
|
20天前
|
存储 缓存 NoSQL
一篇搞懂!Java对象序列化与反序列化的底层逻辑
本文介绍了Java中的序列化与反序列化,包括基本概念、应用场景、实现方式及注意事项。序列化是将对象转换为字节流,便于存储和传输;反序列化则是将字节流还原为对象。文中详细讲解了实现序列化的步骤,以及常见的反序列化失败原因和最佳实践。通过实例和代码示例,帮助读者更好地理解和应用这一重要技术。
19 0
|
6月前
|
XML JavaScript Java
详解Java解析XML的四种方法
详解Java解析XML的四种方法
338 1
|
XML Java 数据格式
java之DocumentBuilderFactory解析xml(2)
java之DocumentBuilderFactory解析xml(2)
267 0
|
XML JavaScript Java
java之DocumentBuilderFactory解析xml(1)
java之DocumentBuilderFactory解析xml(1)
420 0
|
XML Java 数据格式