spring+oxm(castor)

本文介绍了如何在Spring框架中使用OXM模块与Castor进行XML对象映射。详细步骤包括引入必要的jar包,创建实体类,配置Spring XML文件,设定mapping文件,以及编写测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.所需jar包

castor-core-1.3.1
castor-xml-1.3.1
spring-oxm-4.0.9.RELEASE

2.实体类 

package com.xwj.Xstream;

import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author yuki
 * @Date 2018/9/16 20:17
 * @Version 1.0
 **/
public class XstreamEntity {
    private int id;
    private String name;
    private Date time;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}

3.spring配置文件 

<bean id="springOxm" class="com.xwj.Xstream.SpringOXMTest"
		  p:marshaller-ref="castorMarshaller"
		  p:unmarshaller-ref="castorMarshaller"/>
	<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"
		  p:mappingLocation="classpath:mapping.xml"/>

4.mapping文件

<mapping>
	<class name="com.xwj.Xstream.XstreamEntity">
		<!--生成xml文件根节点名字-->
		<map-to xml="xstreamEntity"/>
		<field name="id" type="integer">
			<bind-xml name="id" node="element"/>
		</field>
		<field name="name" type="string">
			<bind-xml name="name" node="element"/>
		</field>
		<field name="time" type="date">
			<bind-xml name="time" node="element"/>
		</field>
	</class>
</mapping>

5.测试文件

package com.xwj.Xstream;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description TODO
 * @Author yuki
 * @Date 2018/9/18 16:35
 * @Version 1.0
 **/
public class SpringOXMTest {
    private Marshaller marshaller;

    public Marshaller getMarshaller() {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    }

    public Unmarshaller getUnmarshaller() {
        return unmarshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller) {
        this.unmarshaller = unmarshaller;
    }

    private Unmarshaller unmarshaller;
    public static XstreamEntity getEntity(){
        XstreamEntity entity=new XstreamEntity();
        entity.setId(123123);
        entity.setName("xwj");
        entity.setTime(new Date());
        return entity;
    }
    public void objToXml() throws IOException {
        XstreamEntity entity=getEntity();
        FileOutputStream out=new FileOutputStream("D:\\test\\test2.xml");
        this.marshaller.marshal(entity,new StreamResult(out));
    }
    public void xmlToObj() throws IOException {
        FileInputStream in=new FileInputStream("D:\\test\\test2.xml");
        XstreamEntity entity= (XstreamEntity) this.unmarshaller.unmarshal(new StreamSource(in));
        System.out.println(entity.getName());
    }

    public static void main(String[] args) throws IOException {
        ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        SpringOXMTest springOXMTest= (SpringOXMTest) context.getBean("springOxm");
        springOXMTest.objToXml();
        springOXMTest.xmlToObj();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值