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();}
}

spring+oxm(castor)相关推荐

  1. 【Spring】Spring oxm 入门初尝试

    O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Mapper.O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML.它的目的是在 Java 对象(几乎 ...

  2. Spring oxm 入门

    oxm : 是spring 3.0的一个新特性 o/xMapping 什么是o/xMapping?             o/x mapping 入门:                     功能 ...

  3. Spring OXM

    Spring OXM简介 O/X Mapping 是什么? Spring 3.0 的一个新特性是 O/X Mapper.然而O/X Mapping并不是新鲜的事物,Spring也是调用了第三方的O/X ...

  4. Spring OXM使用体验

    本文转载自http://www.ibm.com/developerworks/cn/xml/x-springXOM/ O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Ma ...

  5. Spring oxm入门实例

    O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...

  6. ClassNotFoundException:org.exolab.castor.xml.XMLException

    问题 在Spring OXM(对象XML映射)中,将对象转换为XML文件时,会遇到以下错误消息: Caused by: java.lang.ClassNotFoundException: org.ex ...

  7. Spring 3.0 OXM - Spring 3.0 框架新特性

    什么是OXM? OXM是Object-to-XML-Mapping的缩写,它是一个O/M-mapper,将java对象映射成XML数据,或者将XML数据映射成java对象.它类似 XML-Marsha ...

  8. spring 3.0 OXM

    本文转载自http://www.ibm.com/developerworks/cn/xml/x-springXOM/ O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Ma ...

  9. Spring MVC与JAX-RS比较与分析

    http://www.infoq.com/cn/articles/springmvc_jsx-rs 过去几年,REST逐渐成为影响Web框架.Web协议与Web应用设计的重要概念.如果你还不了解RES ...

最新文章

  1. matlab paticalcoff,关于DOA估计中加权前后向空间平滑算法的仿真问题
  2. python break -else 语句
  3. 机器人演唱邓丽君是真的吗_体验官|炒菜机器人真的实用吗
  4. [PAPER-RECORD]
  5. Linux备份压缩命令
  6. android blcr 编译,BLCR 基本环境搭建【zz~】
  7. Shader 坐标转换
  8. MySQL json-table-functions
  9. queueMicrotask 来执行微任务
  10. java svn插件_eclipse 安装SVN插件 subversive
  11. python中nums[:]和nums
  12. 【数据库原理及应用】——数据库设计(学习笔记)
  13. 关于渲染帧率(FPS)的问题
  14. duxing201606很快乐
  15. 串流直播流媒体视频发布平台功能模块和产品技术参数
  16. 中国互联网络信息中心域名争议解决办法
  17. 华为OD机试 - 执行时长(Java JS Python)
  18. 如何关闭手机提醒事项的推送
  19. demo对接php,喜马拉雅数据API 接入 demo PHP版 API
  20. 小花仙攻略 各种花成熟时间和经验点

热门文章

  1. python 3d打印_基于Python的结构拓扑优化与3D打印试验研究
  2. IRF学习笔记-配置
  3. 关于虚拟化层-Windows镜像的测试
  4. RedFlag 6.0 SP1的一些设置
  5. QQ、MSN、淘包旺旺、Skype常设对话的html链接代码
  6. 从零开始学习Linux运维,成为IT领域翘楚(三)
  7. 【人工智能】什么是数据标注?
  8. ps处理几亿个像素点的照片时,如何保存为几十兆而又很清晰
  9. 基于esp8266的Arduino网络时钟
  10. 侧信道攻击:SPA / DPA