|
|
在mq的数据传输过程中,往往将java bean与xml进行相互转换。
castor是exolab group下面的一个开放源代码的项目,提供了java bean与xml之间相互转换的功能。
他提供默认方式,也支持用户diy。
<span style="color: #ff0000;">默认方式:</span>
<span style="color: #ff0000;">1. 基本类型属性:int,boolean等以属性的方式输出</span>
<span style="color: #ff0000;">2. 对象类型属性:以子元素的方式输出</span>
一. 实例
导入包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() */@overridepublic string tostring() {// todo auto-generated method stubreturn "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();<span style="color: #000000;">marshaller.marshal(stusrc, result);</span>system.out.println(result); system.out.println("==========================================================");<span style="color: #000000;">student studist = (student) unmarshaller.unmarshal(student.class, new stringreader(result.tostring()));</span>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/ |
|