xml与实体类的转换

package com.ibbc.mbank.remote.util;

import com.ibbc.mbank.remote.dto.*;
import com.ibbc.mbank.remote.service.ImpI.RepayServiceImpl;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.slf4j.LoggerFactory;

/**

  • XStream工具类

  • @author sun.kai

  • 2016年8月13日
    */
    public class XStreamUtil {

    private static XStream xStream;
    private static String QueryBillInfoVO = “5109”;
    private static String QueryBillListVO = “5108”;
    private static String QueryRepayPlanVO = “5110”;
    private static String RepayVO = “5201”;
    private static String VerifycodeVO = “3B00”;

    //xml转java对象
    public static Object xmlToBean(String xml, String type) {
    xStream = new XStream(new DomDriver());
    xStream.alias(“Head”, HeadRes.class);
    if (type.equals(QueryBillInfoVO)) {
    xStream.alias(“Msg”, QueryBillInfoVO.class);
    xStream.alias(“Body”, QueryBillInfoRes.class);
    } else if (type.equals(QueryBillListVO)) {
    xStream.alias(“Msg”, QueryBillListVO.class);
    xStream.alias(“Body”, QueryBillListRes.class);
    xStream.alias(“BorrowingList”, QueryBill.class);
    xStream.addImplicitCollection(QueryBillListRes.class, “list”); // 去掉集合标签
    } else if (type.equals(QueryRepayPlanVO)) {
    xStream.alias(“Msg”, QueryRepayPlanVO.class);
    xStream.alias(“Body”, QueryRepayPlanRes.class);
    xStream.alias(“RePayMentList”, QueryRepayPlan.class);
    xStream.addImplicitCollection(QueryRepayPlanRes.class, “list”);
    } else if (type.equals(RepayVO)) {
    xStream.alias(“Msg”, RepayVO.class);
    xStream.alias(“Body”, RepayRes.class);
    } else if (type.equals(VerifycodeVO)) {
    xStream.alias(“Msg”, VerifycodeVO.class);
    xStream.alias(“Body”, VerifycodeRes.class);

     }
     return xStream.fromXML(xml);
    

    }

    //java对象转xml
    public static String beanToXml(Object obj) {
    xStream = new XStream(new DomDriver());
    /*
    * 使用xStream.alias(String name, Class Type)为任何一个自定义类创建到类到元素的别名
    * 如果不使用别名,则生成的标签名为类全名
    */
    xStream.alias(“Msg”, QueryRepayPlanBO.class);
    xStream.alias(“Msg”, QueryBillInfoBO.class);
    xStream.alias(“Msg”, RepayBO.class);
    xStream.alias(“Msg”, VerifycodeBO.class);
    xStream.alias(“Msg”, QueryBillListBO.class);
    xStream.alias(“Head”, HeadReq.class);
    xStream.alias(“Body”, QueryRepayPlanReq.class);
    xStream.alias(“Body”, QueryBillInfoReq.class);
    xStream.alias(“Body”, QueryBillListReq.class);
    xStream.alias(“Body”, RepayReq.class);
    xStream.alias(“Body”, VerifycodeReq.class);
    return “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n” + xStream.toXML(obj);
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值