SAXBuilder解析xml檔案內容用於公共引數的校驗
阿新 • • 發佈:2019-01-09
SAXBuilder解析xml檔案內容
xml檔案:
<?xml version="1.0" encoding="UTF-8" ?>
<IELPM name="快捷支付-綁卡支付">
<merIn>
<merchantNo length="16" must="y" mer="MERCHANTNO"></merchantNo>
<version length="20" must="n" mer="VERSION"></version>
<channelNo length="50" must="n" mer="CHANNELNO"></channelNo>
<tranCode length="6" must="y" mer="TRANCODE"></tranCode>
<tranFlow length="32" must="y" mer="TRANFLOW"></tranFlow>
<tranDate length="8" must="y" mer="TRANDATE"></tranDate>
<tranTime length="6" must="y" mer="TRANTIME"></tranTime>
<bindId length="50" must="y" mer="BINDID"></bindId>
<amount length="16" must="y" mer="AMOUNT"></amount>
<payType length="5" must="y" mer="PAYTYPE"></payType>
<remark length="100" must="y" mer="REMARK"></remark>
<notifyUrl length="300" must="y" mer="NOTIFYURL"></notifyUrl>
<bizType length="5" must="y" mer="BIZTYPE"></bizType>
<goodsName length="100" must="y" mer="GOODSNAME"></goodsName>
<goodsInfo length="100" must="n" mer="GOODSINFO"></goodsInfo>
<goodsNum length="100" must="n" mer="GOODSNUM"></goodsNum>
<buyerName length="256" must="y" mer="BUYERNAME"></buyerName>
<contact length="256" must="n" mer="CONTACT"></contact>
<buyerId length="100" must="y" mer="BUYERID"></buyerId>
<ext1 length="500" must="n" mer="EXT1"></ext1>
<ext2 length="500" must="n" mer="EXT2"></ext2>
<YUL1 length="500" must="n" mer="YUL1"></YUL1>
<YUL2 length="500" must="n" mer="YUL2"></YUL2>
<YUL3 length="500" must="n" mer="YUL3"></YUL3>
</merIn>
</IELPM>
/**
* 驗證交易明細資料 一種新穎的方法
*
* @param context 資料內容
* @param fileName
* @return
*/
@SuppressWarnings("unchecked")
public static SMCode analysisTranData(Context context, String fileName, Map<String, String> param){
logger.info("商戶請求交易資料...");
SAXBuilder builder = new SAXBuilder(false);
FileInputStream fis = null;
try {
String path = SMMsgUtil.class.getClassLoader().getResource("").toURI().getPath();
fis = new FileInputStream(new File(path + fileName));
/* 獲取目錄資料 */
Document document = builder.build(fis);
List<Element> bodys = document.getRootElement().getChild("merIn").getChildren();
String fieldName = ""; // 域名
String fieldValue = ""; // 域值
int idx = 1; // 序號
for (Element element : bodys) { // 遍歷XML
String length = element.getAttributeValue("length");
String must = element.getAttributeValue("must");
String sqlName = element.getAttributeValue("mer");
fieldName = element.getName();
fieldValue = StringUtils.defaultString(param.get(fieldName)).trim();
logger.info("Field["+ idx +"]<"+ fieldName +">:" + fieldValue);
if("y".equals(must) && "".equals(fieldValue)){ // 判斷是否為空
logger.info("必填欄位未上送:Field["+ idx +"]"+ fieldName);
return SMCode.C0026;
}else{
if(fieldValue.length() > Integer.parseInt(length)){ // 判斷長度
logger.error("報文域長度不符:Field["+ idx +"]"+ fieldName);
return SMCode.C0026;
}
}
context.setFieldValue(sqlName, fieldValue); // 新增到Context中
idx++;
}
return SMCode.C0000;
} catch (Exception e) {
logger.error("解析商戶請求報文異常", e);
return SMCode.C9999;
}
}
xml檔案:
<?xml version="1.0" encoding="UTF-8" ?>
<IELPM name="快捷支付-綁卡支付">
<merIn>
<merchantNo length="16" must="y" mer="MERCHANTNO"></merchantNo>
<version length="20" must="n" mer="VERSION"></version>
<channelNo length="50" must="n" mer="CHANNELNO"></channelNo>
<tranCode length="6" must="y" mer="TRANCODE"></tranCode>
<tranFlow length="32" must="y" mer="TRANFLOW"></tranFlow>
<tranDate length="8" must="y" mer="TRANDATE"></tranDate>
<tranTime length="6" must="y" mer="TRANTIME"></tranTime>
<bindId length="50" must="y" mer="BINDID"></bindId>
<amount length="16" must="y" mer="AMOUNT"></amount>
<payType length="5" must="y" mer="PAYTYPE"></payType>
<remark length="100" must="y" mer="REMARK"></remark>
<notifyUrl length="300" must="y" mer="NOTIFYURL"></notifyUrl>
<bizType length="5" must="y" mer="BIZTYPE"></bizType>
<goodsName length="100" must="y" mer="GOODSNAME"></goodsName>
<goodsInfo length="100" must="n" mer="GOODSINFO"></goodsInfo>
<goodsNum length="100" must="n" mer="GOODSNUM"></goodsNum>
<buyerName length="256" must="y" mer="BUYERNAME"></buyerName>
<contact length="256" must="n" mer="CONTACT"></contact>
<buyerId length="100" must="y" mer="BUYERID"></buyerId>
<ext1 length="500" must="n" mer="EXT1"></ext1>
<ext2 length="500" must="n" mer="EXT2"></ext2>
<YUL1 length="500" must="n" mer="YUL1"></YUL1>
<YUL2 length="500" must="n" mer="YUL2"></YUL2>
<YUL3 length="500" must="n" mer="YUL3"></YUL3>
</merIn>
</IELPM>
/**
* 驗證交易明細資料 一種新穎的方法
*
* @param context 資料內容
* @param fileName
* @return
*/
@SuppressWarnings("unchecked")
public static SMCode analysisTranData(Context context, String fileName, Map<String, String> param){
logger.info("商戶請求交易資料...");
SAXBuilder builder = new SAXBuilder(false);
FileInputStream fis = null;
try {
String path = SMMsgUtil.class.getClassLoader().getResource("").toURI().getPath();
fis = new FileInputStream(new File(path + fileName));
/* 獲取目錄資料 */
Document document = builder.build(fis);
List<Element> bodys = document.getRootElement().getChild("merIn").getChildren();
String fieldName = ""; // 域名
String fieldValue = ""; // 域值
int idx = 1; // 序號
for (Element element : bodys) { // 遍歷XML
String length = element.getAttributeValue("length");
String must = element.getAttributeValue("must");
String sqlName = element.getAttributeValue("mer");
fieldName = element.getName();
fieldValue = StringUtils.defaultString(param.get(fieldName)).trim();
logger.info("Field["+ idx +"]<"+ fieldName +">:" + fieldValue);
if("y".equals(must) && "".equals(fieldValue)){ // 判斷是否為空
logger.info("必填欄位未上送:Field["+ idx +"]"+ fieldName);
return SMCode.C0026;
}else{
if(fieldValue.length() > Integer.parseInt(length)){ // 判斷長度
logger.error("報文域長度不符:Field["+ idx +"]"+ fieldName);
return SMCode.C0026;
}
}
context.setFieldValue(sqlName, fieldValue); // 新增到Context中
idx++;
}
return SMCode.C0000;
} catch (Exception e) {
logger.error("解析商戶請求報文異常", e);
return SMCode.C9999;
}
}