`
orange5458
  • 浏览: 347496 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Castor (一) -- 默认绑定

阅读更多

在MQ的数据传输过程中,往往将JAVA BEAN与XML进行相互转换。

 

Castor是ExoLab Group下面的一个开放源代码的项目,提供了JAVA BEAN与XML之间相互转换的功能。

他提供默认方式,也支持用户DIY。

 

默认方式:

1. 基本类型属性:int,boolean等以属性的方式输出

2. 对象类型属性:以子元素的方式输出

 

一. 实例

 

导入包castor-1.2-xml.jar以及它的依赖包commons-logging-1.1.1.jar,xerces.jar(xerces-1_4_4)

注:现在最新的castor版本为1.3.2,但是是JDK6环境下编译的,所以需要在JDK6下才能运行

 

Address.java

 

package com.siyuan.castor;

public class Address {
 
 private String street;

 /**
  * @return the street
  */
 public String getStreet() {
  return street;
 }

 /**
  * @param street the street to set
  */
 public void setStreet(String street) {
  this.street = street;
 }

 /* (non-Javadoc)
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  // TODO Auto-generated method stub
  return "Address[street=" + street +"]";
 }
 
}

  

Student.java

 

package com.siyuan.castor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {
	
	private int age;
	
	private boolean male;
	
	private String name;

	private Address address;
	
	private Date birthday;
	
	private Student girlFriend;
	
	private Set<Student> friends = new HashSet<Student>();
	
	private List<String> subjects = new ArrayList<String>();
	
	private Map<String, String> teachers = new HashMap<String, String>(); 
	

	public Student() {
		
	}
	
	public Student(String name) {
		this.name = name;
	}
	
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the mail
	 */
	public boolean isMale() {
		return male;
	}

	/**
	 * @param mail the mail to set
	 */
	public void setMale(boolean male) {
		this.male = male;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public Address getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(Address address) {
		this.address = address;
	}
	
	/**
	 * @return the friends
	 */
	public Set<Student> getFriends() {
		return friends;
	}

	/**
	 * @param friends the friends to set
	 */
	public void setFriends(Set<Student> friends) {
		this.friends = friends;
	}
	
	public void addFriend(Student friend) {
		friends.add(friend);
	}

	/**
	 * @return the girlFriend
	 */
	public Student getGirlFriend() {
		return girlFriend;
	}

	/**
	 * @param girlFriend the girlFriend to set
	 */
	public void setGirlFriend(Student girlFriend) {
		this.girlFriend = girlFriend;
	}

	/**
	 * @return the subjects
	 */
	public List<String> getSubjects() {
		return subjects;
	}

	public void addSubject(String subject) {
		subjects.add(subject);
	}

	/**
	 * @return the teachers
	 */
	public Map<String, String> getTeachers() {
		return teachers;
	}
	
	/**
	 * @return the teachers
	 */
	public void setTeachers(Map<String, String> teachers) {
		this.teachers = teachers;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Student[name=" + name + ",age=" + age + ",male=" + male + ",birthday=" + birthday +",\n"
			+ "address=" + address + ",\n" 
			+ "girlFriend=" + girlFriend + ",\n"
			+ "friends=" + friends + ",\n" 
			+ "subjects=" + subjects + ",\n" 
			+ "teachers=" + teachers + "\n" 
			+ "]";
	}
	
}

 

CastorTest.java

 

package com.siyuan.castor.test;

import java.io.StringReader;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

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 com.siyuan.castor.Address;
import com.siyuan.castor.Student;

public class CastorTest {

	/**
	 * @param args
	 * @throws ValidationException 
	 * @throws MarshalException 
	 * @throws ValidationException 
	 * @throws MarshalException 
	 */
	public static void main(String[] args) throws MarshalException, ValidationException{
		Student stuSrc = new Student();
		stuSrc.setAge(22);
		stuSrc.setName("SingleMan");
		stuSrc.setMale(true);
		Address address = new Address();
		address.setStreet("Renmin Road");
		stuSrc.setAddress(address);
		DateFormat dateFmt = new SimpleDateFormat("yyyy-MM-DD");
		try {
			Date birthday = dateFmt.parse("1988-11-21");
			stuSrc.setBirthday(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Student girl = new Student();
		girl.setAge(20);
		stuSrc.setGirlFriend(girl);
		
		Set<Student> students = new HashSet<Student>();
		Student stu1 = new Student();
		stu1.setAge(21);
		students.add(stu1);
		Student stu2 = new Student();
		stu2.setAge(23);
		students.add(stu2);
		
		stuSrc.addSubject("English");
		stuSrc.addSubject("Math");
		stuSrc.addSubject("Chinese");
		
		Map<String, String> teachers = new HashMap<String, String>();
		teachers.put("English", "teacher a");
		teachers.put("Chinese", "teacher b");
		stuSrc.setTeachers(teachers);
		
		StringWriter result = new StringWriter();
		Marshaller.marshal(stuSrc, result);
		System.out.println(result);
		
		  System.out.println("==========================================================");
		
		Student stuDist = (Student) Unmarshaller.unmarshal(Student.class, new StringReader(result.toString()));
		System.out.println(stuDist);
	}

}

 

2.运行结果

 

<?xml version="1.0" encoding="UTF-8"?>
<student male="true" age="22"><name>SingleMan</name><teachers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:org.exolab.castor.mapping.MapItem"><key xsi:type="java:java.lang.String">English</key><value xsi:type="java:java.lang.String">teacher a</value></teachers><teachers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:org.exolab.castor.mapping.MapItem"><key xsi:type="java:java.lang.String">Chinese</key><value xsi:type="java:java.lang.String">teacher b</value></teachers><girl-friend male="false" age="20"/><subjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">English</subjects><subjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Math</subjects><subjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Chinese</subjects><address><street>Renmin Road</street></address><birthday>1988-01-21T00:00:00.000+08:00</birthday></student>
============================================================
Student[name=SingleMan,age=22,male=true,birthday=Thu Jan 21 00:00:00 CST 1988,
address=Address[street=Renmin Road],
girlFriend=Student[name=null,age=20,male=false,birthday=null,
address=null,
girlFriend=null,
friends=[],
subjects=[],
teachers={}
],
friends=[],
subjects=[English, Math, Chinese],
teachers={English=teacher a, Chinese=teacher b}
]

  

3.参考资料

 

http://www.ibm.com/developerworks/cn/xml/x-bindcastor/

分享到:
评论

相关推荐

    Castor Plug-in for Eclipse 图解使用教程

    Castor Plug-in for Eclipse 插件的使用总结,全部以截图的方式一步一步向下引导。从安装开始,到使用其产生类。这是我的学习文档,写的比较的简单,多是一些图。目的就是为了让自己以后忘了的时候,能够一眼就看懂...

    castor-1.2-xml-schema castor转化XML需要的jar包

    Castor 项目采用 BSD 类型的证书,因此可在任何类型的应用程序(包括完整版权的项目)中使用。 Castor 实际上仅仅有 XML 数据绑定,它还支持 SQL 和 LDAP 绑定

    castor-1.2系列.rar

    castor-1.2.jar castor-1.2-anttasks.jar castor-1.2-codegen.jar castor-1.2-ddlgen.jar castor-1.2-jdo.jar castor-1.2-xml-schema.jar castor-1.2-xml.jar

    castor-1.0-xml.jar

    org.castor.util.IdentityMap org.castor.util.IdentitySet org.exolab.javasource.JEnum org.exolab.javasource.JType org.exolab.castor.util.List org.exolab.javasource.Header org.exolab.javasource.JClass ...

    castor-xml-1.3.2.jar

    Unmarshall与Marshall使用的castor-xml-1.3.2.jar包

    castor-core-1.3.2

    Unmarshall与Marshall使用的jar包

    castor-1.2-examples

    castor-1.2-examples

    castor-1.2.zip

    castor-1.2.zip

    castor1-2(java and xml 数据绑定过程所需数据包)

    利用该包内提供的java类,可以进行java读取解析xml文件,并对文件进行操作

    castor-1.2-doc.zip

    castor-1.2-doc.zip

    castor-0.9.5.2.jar

    castor-0.9.5.2.jar

    castor-1.2-codegen castor代码生成需要的jar包

    Castor 项目采用 BSD 类型的证书,因此可在任何类型的应用程序(包括完整版权的项目)中使用。 Castor 实际上仅仅有 XML 数据绑定,它还支持 SQL 和 LDAP 绑定

    castor1.3 完整jar包

    完整jar 直接导入使用 Castor 是一种将Java对象和XML自动绑定的开源软件。它可以在Java对象、XML文本、SQL数据表以及LDAP目录之间绑定。

    castor-0.9.9.1.jar

    Castor的jar包 对XML的序列号更容易些!

    castor-0.9.5.3-xml.jar

    castor-0.9.5.3-xml.jar,java和xml互相转换所使用的JAR

    castor实现xsd生成javabean所需jar

    java -classpath D:\xsd/castor-1.2-anttasks.jar;D:\xsd/castor-1.2-codegen.jar;D:\xsd/commons-logging-1.1.jar;D:\xsd/castor-1.2-ddlgen.jar;D:\xsd/castor-1.2-jdo.jar;D:\xsd/castor-1.2-xml-schema.jar;D:\...

    Java 对象XML解析器castor-0.9

    要实现的是O/R映射功能。它主要API和数据接口为:JDO-like, SQL, OQL, JDBC, LDAP, XML, DSML。它支持分布式目录事务处理和时间;提供处理XML、Directory、XADirectory的类库,提供从XML到JAVA类的转换机制。

    castor-1.2解析XML

    1、xml格式文件的用法,我们学过的xml有两种用法:一,作为配置文件。二、作为数据存储文件。 其中作为数据存储文件有很多优点,比如,数据更加规范,数据校验等。 2、这个工具的功能:能够把xml文件存储的数据和...

    castor-0.9.9.zip

    astorastorastorastorastorastorastor

    castor-xml.jar

    解析wsdl文件的好帮手,和jdom.jar,wsdl4j.jar组合,能很好的解吸wsdl文件。

Global site tag (gtag.js) - Google Analytics