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