zoukankan      html  css  js  c++  java
  • 将某个类封装成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);
    //     }
    
    }
  • 相关阅读:
    减绳子 [二分查找]
    *数据删除*OJ的原题大赏
    多项式大总结
    【YBTOJ】【Luogu P6218】[USACO06NOV] Round Numbers S
    【YBTOJ】【HDU3652】B-number
    【Luogu P5752】[NOI1999] 棋盘分割
    【YBTOJ】【UVA10559】方块消除 Blocks
    【YBTOJ】【Luogu P5020】[NOIP2018 提高组] 货币系统
    【YBTOJ】【Luogu P4180】[BJWC2010]严格次小生成树
    【YBTOJ】【Luogu P2680】[NOIP2015 提高组] 运输计划
  • 原文地址:https://www.cnblogs.com/luzhanshi/p/10754603.html
Copyright © 2011-2022 走看看