将某个类封装成XML形式返回

<?xml version="1.0" encoding="GBK" standalone="no"?><package><pub><txcode>2013</txcode><appno>999</appno></pub><req><devid>0093002</devid><tellerno>admin</tellerno><type>1</type><cardtype>02</cardtype></req></package>
具体实现原理:类的toString()方法里面进行装配:
参考代码:
public class ServerMessage {
/** 交易码(必须) */
private String txcode = null;
/** 返回码 */
private String retcode = null;
/** 返回信息 */
private String retmsg = null;
/** 应用号(必须) */
private String appno = null;
/** 设备编号(必须) */
private String devid = null;
/** 柜员号 */
private String tellerno = null;
/** 卡号 */
private String cardno = null;
private String cardno2 = null;
/** 持卡人姓名 */
private String perName;
/** 持卡人身份证 */
private String perNo;
/** 社保卡号 */
private String siCardNo;
/**
* 盘库类型(1全部;2按指定层指定槽盘层;3从指定位置全部;4指定槽)
* 下载登记簿类型(01卡登记簿02进卡登记簿03取卡登记簿04进取卡登记簿05故障登记簿06故障卡槽登记簿07进取卡统计报表)
*/
private String type = null; // 出卡类型 或者进卡类型
/** 盘库——第几层开始盘 */
private String num = null;
/** 盘库——第几卡槽开始盘 */
private String position = null;
/** 盘库——序号 */
private String serial = null;
/** 文件路径 */
private String filepath = null;
/** 操作日期 */
private String operDate = null;
private boolean reqStatus = false; // <req>标签标记
/** 授权柜员号 */
private String author = null;
/** 开始时间 */
private String startDate = null;
/** 结束时间 */
private String endDate = null;
/** 卡种 */
private String cardtype = null;
public ServerMessage() {
this.appno = ProjectConfig.getInstance().getAppno(); // 获取应用号
}
public ServerMessage(String txcode, String devid) {
this.txcode = txcode;
this.devid = devid;
this.appno = ProjectConfig.getInstance().getAppno(); // 获取应用号
}
public ServerMessage(String txcode, String devid, String tellerno) {
this(txcode, devid);
this.tellerno = tellerno;
}
/**
* 创建同步请求消息
*/
public static ServerMessage createSyncMessage(String txcode, String devid, String tellerno) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setReqStatus(true);
return msg;
}
/**
* 创建批量取卡消息
*/
public static ServerMessage createTakeCardMessage(String txcode, String devid, String tellerno, String cardno, String perNo, String perName, String siCardNo, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setCardno(cardno);
msg.setPerNo(perNo);
msg.setPerName(perName);
msg.setSiCardNo(siCardNo);
msg.setReqStatus(true);
return msg;
}
/**
* 创建盘库消息
*/
public static ServerMessage createPankuMessage(String txcode, String devid, String tellerno, String type, String num, String position,
String serial, String filepath) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setNum(num);
msg.setPosition(position);
msg.setSerial(serial);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
}
/**
* 创建上传终端日志消息
*/
public static ServerMessage createUploadLogMessage(String txcode, String devid, String tellerno, String operDate, String filepath) {
String date = operDate.replace("-", ""); // 接口的日期格式是yyyyMMdd
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setOperDate(date);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
}
/**
* 创建状态消息
*/
public static ServerMessage createReplyMessage(String txcode, String retcode, String retmsg) {
ServerMessage msg = new ServerMessage();
msg.setTxcode(txcode);
msg.setRetcode(retcode);
msg.setRetmsg(retmsg);
return msg;
}
/**
* 创建状态消息(进卡/取卡)
*/
public static ServerMessage createReplyMessage(String txcode, String retcode, String retmsg, String cardno2) {
ServerMessage msg = new ServerMessage();
msg.setTxcode(txcode);
msg.setRetcode(retcode);
msg.setRetmsg(retmsg);
msg.setCardno2(cardno2);
return msg;
}
/**
* 创建手工登记消息
*/
public static ServerMessage createRegisterMessage(String txcode, String devid, String tellerno, String cardno, String author, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setCardno(cardno);
msg.setAuthor(author);
msg.setType(type);
msg.setReqStatus(true);
return msg;
}
/**
* 创建下载登记簿消息
*/
public static ServerMessage createDownInfoMessage(String txcode, String devid, String tellerno, String filepath, String type, String startDate,
String endDate) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setFilepath(filepath);
msg.setType(type);
msg.setStartDate(startDate);
msg.setEndDate(endDate);
msg.setReqStatus(true);
return msg;
}
/**
* 创建盘点消息
*/
public static ServerMessage createPandianMessage(String txcode, String devid, String tellerno, String type, String num, String position,
String serial, String filepath) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setNum(num);
msg.setPosition(position);
msg.setSerial(serial);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
}
/**
* 创建根据卡槽取卡消息
*/
public static ServerMessage createTakeCardBySlotMessage(String txcode, String devid, String tellerno, String num, String position) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setNum(num);
msg.setPosition(position);
msg.setReqStatus(true);
return msg;
}
/**
* 创建进卡消息
*/
public static ServerMessage createCardInMessage(String txcode, String devid, String tellerno, String type, String cardtype) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setCardtype(cardtype);
msg.setReqStatus(true);
return msg;
}
/**
* 创建取消进卡消息
*/
public static ServerMessage createUnCardInMessage(String txcode, String devid, String tellerno, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
//msg.setType(type);
msg.setReqStatus(true);
return msg;
}
public String getCardtype() {
return cardtype;
}
public void setCardtype(String cardtype) {
this.cardtype = cardtype;
}
public String getTxcode() {
return txcode;
}
public void setTxcode(String txcode) {
this.txcode = txcode;
}
public String getAppno() {
return appno;
}
public void setAppno(String appno) {
this.appno = appno;
}
public String getDevid() {
return devid;
}
public void setDevid(String devid) {
this.devid = devid;
}
public String getTellerno() {
return tellerno;
}
public void setTellerno(String tellerno) {
this.tellerno = tellerno;
}
public String getCardno() {
return cardno;
}
public void setCardno(String cardno) {
this.cardno = cardno;
}
public String getPerName() {
return perName;
}
public void setPerName(String perName) {
this.perName = perName;
}
public String getPerNo() {
return perNo;
}
public void setPerNo(String perNo) {
this.perNo = perNo;
}
public String getSiCardNo() {
return siCardNo;
}
public void setSiCardNo(String siCardNo) {
this.siCardNo = siCardNo;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
public String getRetcode() {
return retcode;
}
public void setRetcode(String retcode) {
this.retcode = retcode;
}
public String getRetmsg() {
return retmsg;
}
public void setRetmsg(String retmsg) {
this.retmsg = retmsg;
}
public String getOperDate() {
return operDate;
}
public void setOperDate(String operDate) {
this.operDate = operDate;
}
public boolean isReqStatus() {
return reqStatus;
}
public void setReqStatus(boolean reqStatus) {
this.reqStatus = reqStatus;
}
public String getCardno2() {
return cardno2;
}
public void setCardno2(String cardno2) {
this.cardno2 = cardno2;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String toXml() {
StringBuilder buf = new StringBuilder();
buf.append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?>");
buf.append("<package>");
buf.append("<pub>");
buf.append("<txcode>").append(txcode).append("</txcode>");
buf.append("<appno>").append(appno).append("</appno>");
if (retcode != null && retcode.length() > 0) {
buf.append("<retcode>").append(retcode).append("</retcode>");
}
if (retmsg != null && retmsg.length() > 0) {
buf.append("<retmsg>").append(retmsg).append("</retmsg>");
}
buf.append("</pub>");
if (reqStatus) {
buf.append("<req>");
if (devid != null && devid.length() > 0) {
buf.append("<devid>").append(devid).append("</devid>");
}
if (tellerno != null && tellerno.length() > 0) {
buf.append("<tellerno>").append(tellerno).append("</tellerno>");
}
if (cardno != null && cardno.length() > 0) {
buf.append("<cardno>").append(cardno).append("</cardno>");
}
if (type != null && type.length() > 0) {
buf.append("<type>").append(type).append("</type>");
}
if (cardtype != null && cardtype.length() > 0) {
buf.append("<cardtype>").append(cardtype).append("</cardtype>");
}
if (perNo != null && perNo.length() > 0) {
buf.append("<idandname>").append(perNo).append("|").append(perName).append("</idandname>");
}
if (siCardNo != null && siCardNo.length() > 0) {
buf.append("<sicardno>").append(siCardNo).append("</sicardno>");
}
if (num != null && num.length() > 0) {
buf.append("<num>").append(num).append("</num>");
}
if (position != null && position.length() > 0) {
buf.append("<position>").append(position).append("</position>");
}
if (serial != null && serial.length() > 0) {
buf.append("<serial>").append(serial).append("</serial>");
}
if (operDate != null && operDate.length() > 0) {
buf.append("<operDate>").append(operDate).append("</operDate>");
}
if (filepath != null && filepath.length() > 0) {
buf.append("<filepath>").append(filepath).append("</filepath>");
}
if (author != null && author.length() > 0) {
buf.append("<author>").append(author).append("</author>");
}
if (startDate != null && startDate.length() > 0) {
buf.append("<startDate>").append(startDate).append("</startDate>");
}
if (endDate != null && endDate.length() > 0) {
buf.append("<endDate>").append(endDate).append("</endDate>");
}
buf.append("</req>");
}
if (cardno2 != null && cardno2.length() > 0) {
buf.append("<ans><cardno>").append(cardno2).append("</cardno></ans>");
}
buf.append("</package>");
return buf.toString();
}
public String toString() {
return toXml();
}
// public static void main(String[] args) {
// ServerMessage msg = createDownInfoMessage("2009", "001", "400085470", "/aaa/bbb/ccc", "01", "2015-11-12", "2015-11-14");
// System.out.println(msg);
// }
}
将某个类封装成XML形式返回的更多相关文章
- java类封装成dll
@参考文章1,@参考文章2,@参考文章3 1,建立测试类,注意英文注释部分,用汉语直接编译会乱码 public class Hello { //native method is used for ca ...
- ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法
可以采用值做映射,也可以不采用映射方式 <resultMap id="configMap" type="java.util.Map" > <r ...
- 把jQuery的类、插件封装成seajs的模块的方法
这篇文章主要介绍了把jQuery的类.插件封装成seajs的模块的方法,需要的朋友可以参考下 注:本文使用的seajs版本是2.1.1 一.把Jquery封装成seajs的模块 define(func ...
- 将bean转换成XML字符串
package com.sinoservices.bms.bbl.rest.bean; import javax.xml.bind.annotation.XmlAccessType; import j ...
- C#实体类(复杂类)与XML互相转换
实体类转换成XML方法: 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T ...
- java将类和函数封装成jar,然后在别的项目中使用这个jar包
本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...
- java将类和函数封装成jar
本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...
- 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...
- Directx11学习笔记【二】 将HelloWin封装成类
我们把上一个教程的代码封装到一个类中来方便以后的使用. 首先新建一个空工程叫做MyHelloWin,添加一个main.cpp文件,然后新建一个类叫做MyWindow,将于窗体有关的操作封装到里面 My ...
随机推荐
- 杂项-EMS:CRM
ylbtech-杂项-EMS:CRM CMS是"Content Management System"的缩写,意为"内容管理系统". 内容管理系统是企业信息化建设 ...
- Sql server 2008 R2 正在关闭[0x80041033]
1. 事件起因, 昨天还访问的好好的, 然后系统一更新, 今天访问的时候, 就报什么 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是 ...
- rpm yum 等命令无响应的解决方法
yum 安装查询任何东西, rpm 安装查询任何东西,执行后无任何反应,直接卡住,也没任何错误信息给出,只能杀掉进程 # yum install XXXX # yum clean all # rpm ...
- Maven+Mybatis+Spring+SpringMVC实现分页查询
转载:http://www.cnblogs.com/zhangtan/p/5846955.html 一.项目搭建 关于项目搭建,小宝鸽以前写过一篇Spirng+SpringMVC+Maven+Myba ...
- ACCESS中如何比较日期和时间,使用DateDiff函数
DateDiff,语法如下:DateDiff( 间隔字符, 日期1, 日期2 [,firstdayofweek[, firstweekofyear]])一般使用 DateDiff( 间隔字符, 日期1 ...
- IOS证书申请 PPF provisioning profile Developer Certificate
[Error] No provisioning profile found for the selected build configuration 新来一个iPhone,真机调试的时候报错. 1.x ...
- webapi的几种过滤器
好久没有写博客了 今天就来聊聊asp.net webapi的过滤器们 过滤器主要有这么几种 AuthorizationFilterAttribute 权限验证 ActionFilterAttribut ...
- java通过反射了解集合泛型的本质
- UNIX 网络编程学习
a.述 书买很久了,好好学习下.O(∩_∩)O 只写程序,原理什么的,先不写了. b.环境 deepin15.4.1 64位 vs code gcc version 6.4.0 20170724 ( ...
- c++ (proxy)代理模式
假设我们有几个具有相似的窗体,都包含关闭窗体(closeButton)和按钮单击事件(ClickButton)我们在处理时,不想直接操作每个窗体,可以请求代理. #include<iostrea ...