Castor引擎

运用Castor引擎实现XML结构的字符串->JavaBean的映射

忘记补上这个了,我现在把我的实现贴出来:
我用的是castor-1.2-xml.jar这个包
测试代码大致如下
public void testCastor() {
		Mapping map = new Mapping();
		String strXml = "<?xml version='1.0' encoding='gb2312'?>" +
	 		"<item_info><item_name>上海迪士尼</item_name>" +
	 		"<district_id>1</district_id><barea>1235</barea>" +
	 		"<plan_cert_id>200801123456</plan_cert_id>" +
	 		"<plan_land_cert_id>200801987654</plan_land_cert_id>" +
	 		"<item_address>南京路</item_address>" +
	 		"<applicant_name>上海投资公司</applicant_name>" +
	 		"<address>南京路</address><post_num>200810256</post_num>" +
	 		"<contact>王先生</contact><contact_phone>110</contact_phone>" +
	 		"<material><name>身份证</name><type>原件</type><copys>1</copys></material>" +
	 		"<material><name>护照</name><type>复印件</type><copys>2</copys></material>" +
	 		"</item_info>";
		try {
			map.loadMapping(this.getClass().getClassLoader().getResource(
					"com/caseflow/domain/mapping.xml"));
			Unmarshaller unmar = new Unmarshaller(map);
			SurvForm survForm = (SurvForm) unmar.unmarshal(SurvForm.class,
					new StringReader(strXml));
			Marshaller marshaller = new Marshaller(new OutputStreamWriter(
					System.out));
			marshaller.setMapping(map);
			marshaller.marshal(survForm);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


其中的SurvForm.java如下
package com.fangdi.caseflow.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class SurvForm implements Serializable {

	/**
	 * long : serialVersionUID
	 */
	private static final long serialVersionUID = -5171139648345487428L;

	private String case_apply_num;

	private String apply_num;

	private int district_id;

	private Vector items;
	
	private int year;

	private int caseno;
	
	private String plan_cert_id;
	
	private String plan_land_cert_id;

	private Date apply_date;

	private String applicant_name;
		
	private String address;

	private String post_num;

	private String contact;

	private String contact_phone;

	private String item_name;

	private String item_address;

	private int item_type;

	private double area;

	private Date overtime;

	private Date enddate;
	
	private String case_d_date;
	
	private String note;
	
	private String archive_info;
	
	private String archive_num;
	
	private String barea;

	private String instance_id;
	
	private Map AttriMAP = new HashMap();

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
		AttriMAP.put("ADDRESS", address);
	}

	public String getApplicant_name() {
		return applicant_name;
	}

	public void setApplicant_name(String applicant_name) {
		this.applicant_name = applicant_name;
		AttriMAP.put("APPLICANT_NAME", applicant_name);
	}

	public Date getApply_date() {
		return apply_date;
	}

	public void setApply_date(Date apply_date) {
		this.apply_date = apply_date;
		AttriMAP.put("APPLY_DATE".toString(), apply_date);
	}

	public String getApply_num() {
		return apply_num;
	}

	public void setApply_num(String apply_num) {
		this.apply_num = apply_num;
		AttriMAP.put("APPLY_NUM", apply_num);
	}

	public String getArchive_info() {
		return archive_info;
	}

	public void setArchive_info(String archive_info) {
		this.archive_info = archive_info;
	}

	public String getArchive_num() {
		return archive_num;
	}

	public void setArchive_num(String archive_num) {
		this.archive_num = archive_num;
		AttriMAP.put("ARCHIVE_NUM", archive_num);
	}

	public double getArea() {
		return area;
	}

	public void setArea(double area) {
		this.area = area;
		AttriMAP.put("AREA", new Double(area));
	}

	public String getBarea() {
		return barea;
	}

	public void setBarea(String barea) {
		this.barea = barea;
		AttriMAP.put("BAREA", new Double(barea));
	}

	public String getCase_apply_num() {
		return case_apply_num;
	}

	public void setCase_apply_num(String case_apply_num) {
		this.case_apply_num = case_apply_num;
	}

	public String getCase_d_date() {
		return case_d_date;
	}

	public void setCase_d_date(String case_d_date) {
		this.case_d_date = case_d_date;
	}

	public int getCaseno() {
		return caseno;
	}

	public void setCaseno(int caseno) {
		this.caseno = caseno;
		AttriMAP.put("CASENO", new Integer(caseno));
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
		AttriMAP.put("CONTACT", contact);
	}

	public String getContact_phone() {
		return contact_phone;
	}

	public void setContact_phone(String contact_phone) {
		this.contact_phone = contact_phone;
		AttriMAP.put("CONTACT_PHONE", contact_phone);
	}

	public int getDistrict_id() {
		return district_id;
	}

	public void setDistrict_id(int district_id) {
		this.district_id = district_id;
		AttriMAP.put("DISTRICT_ID", new Integer(district_id));
	}

	public Date getEnddate() {
		return enddate;
	}

	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}

	public String getInstance_id() {
		return instance_id;
	}

	public void setInstance_id(String instance_id) {
		this.instance_id = instance_id;
	}

	public String getItem_address() {
		return item_address;
	}

	public void setItem_address(String item_address) {
		this.item_address = item_address;
		AttriMAP.put("ITEM_ADDRESS", item_address);
	}

	public String getItem_name() {
		return item_name;
	}

	public void setItem_name(String item_name) {
		this.item_name = item_name;
		AttriMAP.put("ITEM_NAME", item_name);
	}

	public int getItem_type() {
		return item_type;
	}

	public void setItem_type(int item_type) {
		this.item_type = item_type;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Date getOvertime() {
		return overtime;
	}

	public void setOvertime(Date overtime) {
		this.overtime = overtime;
	}

	public String getPost_num() {
		return post_num;
	}

	public void setPost_num(String post_num) {
		this.post_num = post_num;
		AttriMAP.put("POST_NUM", post_num);
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public Map getAttriMAP() {
		return AttriMAP;


}

public void setAttriMAP(Map attriMAP) {
AttriMAP = attriMAP;
}

public String getPlan_cert_id() {
return plan_cert_id;
}

public void setPlan_cert_id(String plan_cert_id) {
this.plan_cert_id = plan_cert_id;
AttriMAP.put("PLAN_CERT_ID", plan_cert_id);
}

public String getPlan_land_cert_id() {
return plan_land_cert_id;
}

public void setPlan_land_cert_id(String plan_land_cert_id) {
this.plan_land_cert_id = plan_land_cert_id;
AttriMAP.put("PLAN_LAND_CERT_ID", plan_land_cert_id);
}

public Vector getItems() {
return items;
}

public void setItems(Vector items) {
this.items = items;
}

}



mapping.xml文件如下
<?xml version="1.0"?>

<mapping>

        <class name="com.fangdi.caseflow.domain.SurvForm"> 
                <map-to xml="item_info"/>
                <field name="item_name" type="java.lang.String">  
                        <bind-xml name="item_name" node="attribute"/>
                </field>
                <field name="district_id" type="integer">  
                        <bind-xml name="district_id" node="attribute"/>
                </field>
                <field name="barea" type="java.lang.String">  
                        <bind-xml name="barea" node="attribute"/>
                </field>
                <field name="plan_cert_id" type="java.lang.String">  
                        <bind-xml name="plan_cert_id" node="attribute"/>
                </field>
                <field name="plan_land_cert_id" type="java.lang.String">  
                        <bind-xml name="plan_land_cert_id" node="attribute"/>
                </field>
                <field name="applicant_name" type="java.lang.String">  
                        <bind-xml name="applicant_name" node="attribute"/>
                </field>
                <field name="address" type="java.lang.String">  
                        <bind-xml name="address" node="attribute"/>
                </field>
                <field name="post_num" type="java.lang.String">  
                        <bind-xml name="post_num" node="attribute"/>
                </field>
                <field name="contact" type="java.lang.String">  
                        <bind-xml name="contact" node="attribute"/>
                </field>
                <field name="contact_phone" type="java.lang.String">  
                        <bind-xml name="contact_phone" node="attribute"/>
                </field>
                <field name="item_address" type="java.lang.String">
                        <bind-xml name="item_address" node= "attribute"/>
                </field>
                <field name="items" type="com.fangdi.caseflow.domain.Material"
                          collection="vector" node= "element">
                        <bind-xml name="material"/>
                </field>
        </class>
        
        <class name="com.fangdi.caseflow.domain.Material">
                <field name="name" type="java.lang.String"
                       direct="true">
                        <bind-xml name="name" node="element"/>
                </field>
                <field name="type" type="java.lang.String"
                       direct="true">
                        <bind-xml name="type" node="element"/>
                </field>
                <field name="copys" type="integer"
                       direct="true">
                        <bind-xml name="copys" node="element"/>
                </field>
        </class>
</mapping>



你可能感兴趣的:(xml)