Velocity组装Xml报文
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Velocity组装Xml报文相关的知识,希望对你有一定的参考价值。
组装报文代码
/** * 请求XML组装 * @param bizNo * @return */ public String createRequestXml(PolicyApplication app,String xmlPath,String resType) throws Exception { String rspXml = ""; try { String userInfo = DeploymentConfig.getProperty("GD_PLAT_USER");//获取平台用户密码 Properties p = new Properties(); p.put("input.encoding", "UTF-8"); p.put("output.encoding", "UTF-8"); p.put("resource.loader", "srl"); p.put("srl.resource.loader.class",FlatXMLTemplateLoader.class.getName()); VelocityEngine veloEngine = new VelocityEngine(); veloEngine.init(p); Template template = veloEngine.getTemplate(xmlPath); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("SEARCH",xmlPath);// 模板路径 BaseDTO baseDto = new BaseDTO(app); velocityContext.put("resType",resType); velocityContext.put("userName",userInfo.split("-")[0]); velocityContext.put("userPass",userInfo.split("-")[1]); velocityContext.put("app",baseDto); StringWriter sw = new StringWriter(); template.merge(velocityContext,sw); sw.close(); rspXml = sw.toString(); } catch (Exception e) { logger.info(e.getMessage()); e.printStackTrace(); } return rspXml; }
报文模版
<?xml version="1.0" encoding="UTF-8"?> <PACKET type="RESPONSE" version="1.0"> <HEAD> <REQUEST_TYPE>$!resType</REQUEST_TYPE> <USER>$!userName</USER> <PASSWORD>$!userPass</PASSWORD> </HEAD> #set($base = $app.getBase()) #set($insured = $app.getInsured()) <BASE_PART> <QUOTE_NO>$!base.CQteNo</QUOTE_NO> <POLICY_CODE>$!insured.CCertfCde</POLICY_CODE> <POLICY_NAME>$!insured.CInsuredNme</POLICY_NAME> <PRODUCT_NO>001</PRODUCT_NO> <PREMIUM_RATE>$!app.getFormatRate($!base.CProdNo,$!base.CTrdCde)</PREMIUM_RATE> <TRANS_DATE>$!app.date2Str(null)</TRANS_DATE> <PRODUCT_LIST> #foreach ($cvrg in $!app.getCoverages()) #if((("010002"==$!cvrg.CCvrgNo) || ("010003"==$!cvrg.CCvrgNo) || ("010004"==$!cvrg.CCvrgNo)) && $!app.isShenzhen($!cvrg.CResvTxt18)) <PRODUCT_COMP> <INDUSTRY_TYPE>$!base.CTrdCde</INDUSTRY_TYPE> <INSURE_CDE>$!app.getAttrNmeBySQL("CCvrgNo",$!cvrg.CCvrgNo)</INSURE_CDE> <LOSS_FREE_RATE>$!app.getFormatDuctRate($!base.NDductRate)</LOSS_FREE_RATE> <LOSS_FREE_AMT>$!base.NDductAmt</LOSS_FREE_AMT> <PREMIUM_RATE>$!app.getFormatRate($!base.CProdNo,$!cvrg.NRate,$!base.CTrdCde)</PREMIUM_RATE> </PRODUCT_COMP> #end #end </PRODUCT_LIST> </BASE_PART> </PACKET>
以上是关于Velocity组装Xml报文的主要内容,如果未能解决你的问题,请参考以下文章
20210311 全网唯一,物联网MQTT协议报文结构分析以及基于C#代码的报文组装实现