javabean 与 xml 互转

2024-06-20 16:48
文章标签 java xml bean 互转

本文主要是介绍javabean 与 xml 互转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目前javaBean和xml进行互相转换的工具有

Xstream,Xmap,JAXB 这三种比较通用

  1. XStream 是什么? 
    XStream 是一个 java类库,实际上是一个转换器。
  2. XStream 作用 ? 
    实现 java bean 与 xml 互转。
  3. XStream 如何使用? 
    主要通过 xStream 实现 转换,具体测试示例代码如下:
  4. 源码 github 下载地址: 
    https://github.com/xiaoyugelicai/TestJackson.git

添加依赖 jar 包

<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream --><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.4</version></dependency>
package testJackson;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import vo.RequestH;
import vo.RequestItem;
import vo.RequestReq;/*** This class was used to test xstream converter, xstream was used to convert javabean to xml or convert xml to javabean.* @author hualei* @date May 3, 2017 9:38:40 AM* */
public class TestXstream {private String xml;/*** convert javabean to xml.* xml 请求数据如下:* <req op="sms_01send">*    <h>*      <ver>1.0</ver>*      <user>test</user>*      <pass>123</pass>*    </h>*    <items>*      <item>*        <id></id>*        <content>短信验证码</content>*        <tele>12345678901</tele>*        <model>1</model>*        <timing>2017-36-03 09:05:25</timing>*        <reply>1</reply>*        <priority>0</priority>*      </item>*    </items>*  </req>*/@Testpublic void javabeanToXml(){RequestReq requestReq = new RequestReq();requestReq.setOp("sms_01send");RequestH requestH = new RequestH();requestH.setVer("1.0");requestH.setUser("test");requestH.setPass("123");RequestItem requestItem = new RequestItem();requestItem.setContent("短信验证码");requestItem.setId("");requestItem.setReply("1");requestItem.setModel("1");requestItem.setPriority("0");requestItem.setTele("12345678901");requestItem.setTiming(new SimpleDateFormat("yyyy-mm-dd hh:MM:ss").format(new Date()));List<RequestItem> items = new ArrayList<RequestItem>();items.add(requestItem);requestReq.setH(requestH);requestReq.setItems(items);XStream xstream = new XStream();// 设置 类型 别名xstream.alias("req", RequestReq.class);xstream.alias("h", RequestH.class);xstream.alias("item", RequestItem.class);// 设置 子标签 别名xstream.aliasField("items", RequestReq.class, "items");// 设置 标签属性 别名xstream.aliasAttribute(RequestReq.class, "op", "op");// javabean to xmlxml = xstream.toXML(requestReq);System.out.println(xml);}/*** convert xml to java bean.*/@Testpublic void xmlToJavabean(){// 给  xml 赋值javabeanToXml();XStream xStream = new XStream();// 设置 类型 别名xStream.alias("req", RequestReq.class);xStream.alias("h", RequestH.class);xStream.alias("item", RequestItem.class);// 设置 子标签 别名xStream.aliasField("items", RequestReq.class, "items");// 设置 标签属性 别名xStream.aliasAttribute(RequestReq.class, "op", "op");// xml to javabeanRequestReq req = (RequestReq)xStream.fromXML(xml);System.out.println(req);// RequestReq [op=sms_01send, h=RequestH [ver=1.0, user=test, pass=123], items=[RequestItem [id=, content=短信验证码, tele=12345678901, model=1, timing=2017-58-03 09:05:22, reply=1, priority=0]]]}
}

实体类 RequestReq.class 代码如下:

package vo;import java.util.Arrays;
import java.util.List;/*** This class was used to generate body message.* @author hualei**/
public class RequestReq {private String op;private RequestH h;private List<RequestItem> items;/*** constructor* @param op* @param h* @param items*/public RequestReq(String op, RequestH h, List<RequestItem> items) {super();this.op = op;this.h = h;this.items = items;}/*** Default constructor*/public RequestReq() {super();}/*** @return the op*/public String getOp() {return op;}/*** @param op the op to set*/public void setOp(String op) {this.op = op;}/*** @return the h*/public RequestH getH() {return h;}/*** @param h the h to set*/public void setH(RequestH h) {this.h = h;}/*** @return the items*/public List<RequestItem> getItems() {return items;}/*** @param items the items to set*/public void setItems(List<RequestItem> items) {this.items = items;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "RequestReq [op=" + op + ", h=" + h.toString() + ", items=" + Arrays.toString(items.toArray()) + "]";}
}

实体类 RequestH.class 代码如下:

package vo;/*** This class was used to generate head content of body message.* @author hualei**/
public class RequestH {/*** The variable was used to receive version.*/private String ver;/*** The variable was used to receive user. */private String user;/*** The variable was used to receive password. */private String pass;/*** Constructor* @param ver* @param user* @param pass*/public RequestH(String ver, String user, String pass) {super();this.ver = ver;this.user = user;this.pass = pass;}/*** Default constructor*/public RequestH() {super();}/*** @return the ver*/public String getVer() {return ver;}/*** @param ver the ver to set*/public void setVer(String ver) {this.ver = ver;}/*** @return the user*/public String getUser() {return user;}/*** @param user the user to set*/public void setUser(String user) {this.user = user;}/*** @return the pass*/public String getPass() {return pass;}/*** @param pass the pass to set*/public void setPass(String pass) {this.pass = pass;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "RequestH [ver=" + ver + ", user=" + user + ", pass=" + pass + "]";}
}
  • 实体类 RequestItem.class 代码如下:
package vo;/*** This class was used to generate item content of body message.* @author hualei**/
public class RequestItem {private String id;private String content;private String tele;private String model;private String timing;private String reply;private String priority;/*** Constructor* @param id* @param content* @param tele* @param model* @param timing* @param reply* @param priority*/public RequestItem(String id, String content, String tele, String model, String timing, String reply,String priority) {super();this.id = id;this.content = content;this.tele = tele;this.model = model;this.timing = timing;this.reply = reply;this.priority = priority;}/*** Default Constructor*/public RequestItem() {super();}/*** @return the id*/public String getId() {return id;}/*** @param id the id to set*/public void setId(String id) {this.id = id;}/*** @return the content*/public String getContent() {return content;}/*** @param content the content to set*/public void setContent(String content) {this.content = content;}/*** @return the tele*/public String getTele() {return tele;}/*** @param tele the tele to set*/public void setTele(String tele) {this.tele = tele;}/*** @return the model*/public String getModel() {return model;}/*** @param model the model to set*/public void setModel(String model) {this.model = model;}/*** @return the timing*/public String getTiming() {return timing;}/*** @param timing the timing to set*/public void setTiming(String timing) {this.timing = timing;}/*** @return the reply*/public String getReply() {return reply;}/*** @param reply the reply to set*/public void setReply(String reply) {this.reply = reply;}/*** @return the priority*/public String getPriority() {return priority;}/*** @param priority the priority to set*/public void setPriority(String priority) {this.priority = priority;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "RequestItem [id=" + id + ", content=" + content + ", tele=" + tele + ", model=" + model + ", timing="+ timing + ", reply=" + reply + ", priority=" + priority + "]";}
}


这篇关于javabean 与 xml 互转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078694

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定