아래 예제는 문자열 파싱하는 예제이다. 실무에 사용하던 소르를 자바로 바꾸어 보았는데..
ㅋㅋ 아직 한글처리가 안됐네요~..
수정해야 겠군요^^
파싱할 원본 문자열 : 8:SESS_KEY20:K01115642091328800012:RT1:06:RT_MSG14:성공하였습니다
10: USAGE_INFO3462:<10:EVENT_TYPE9:CHARGE_CM17:PORTAL_SERVICE_CD2:3019:PORTAL_SERVICE_NAME7:DOSIRAK13:
CONTENTS_DATE14:200608231716468:QUANTITY5:100006:SYMBOL7:KOIN(S) 13:
CONTENTS_NAME15:코인머니1만원권12:SERVICE_TYPE1:F17:SERVICE_TYPE_NAME4:유선11:
CHARGE_TYPE1:A16:CHARGE_TYPE_NAME4:후불12:CHARGING_MDN11:0103216169411:
RESOURCE_ID4:500214:BALANCE_AMOUNT3:10015:TOTAL_REC_COUNT2:10>
문자열 구조는 키 + 키값길이 : 키값 + 키길이
=========================================================
import java.lang.*;
class Parsing {
public String ReturnOK(String str){
String RT_CODE;
String[] ArrStr = str.split("RT1:");
RT_CODE = ArrStr[1].substring(0,1);
if(RT_CODE.equals("0")){
return "성공";
} else {
return "실패:" + RT_CODE;
}
}
public String Pars(String str, String FindStr){
int j = 1, FindLength;
String[] RsltStr;
String[] TmpStr;
StringBuilder ReturnValue = new StringBuilder();
RsltStr = str.split(FindStr);
TmpStr = RsltStr[1].split(":");
for(int i=0; i <= TmpStr[1].length() ; i++){
if( j > Integer.parseInt(TmpStr[0]) ) break;
if( (int)TmpStr[1].charAt(i) > 1000 ){
j+=2;
} else{
j++;
}
//System.out.printf("\n%d %d %c", (int)TmpStr[1].charAt(i), j , TmpStr[1].charAt(i) );
ReturnValue.append(TmpStr[1].charAt(i));
}
return ReturnValue.toString();
}
public static void main(String[] args) {
String ApiRtn = "8:SESS_KEY20:K01115642091328800012:RT1:06:RT_MSG14:성공하였습니다
10: USAGE_INFO3462:<10:EVENT_TYPE9:CHARGE_CM17:PORTAL_SERVICE_CD2:3019:PORTAL_SERVICE_NAME7
:DOSIRAK13:CONTENTS_DATE14:200608231716468:QUANTITY5:100006:SYMBOL7:KOIN(S)
13:CONTENTS_NAME15:코인머니1만원권12:SERVICE_TYPE1:F17:SERVICE_TYPE_NAME4:유선11:
CHARGE_TYPE1:A16:CHARGE_TYPE_NAME4:후불12:CHARGING_MDN11:0103216169411:
RESOURCE_ID4:500214:BALANCE_AMOUNT3:10015:TOTAL_REC_COUNT2:10>";
System.out.printf("원본 문자열 ApiRtn = %s\n", ApiRtn);
Parsing ps = new Parsing();
if(ps.ReturnOK(ApiRtn).equals("성공") ) {
String FindStr = "EVENT_TYPE";
System.out.printf("\n키 : %s \t값 : %s", FindStr, ps.Pars(ApiRtn, FindStr));
System.out.printf("\n키 : %s \t값 : %s", "SERVICE_TYPE_NAME", ps.Pars(ApiRtn, "SERVICE_TYPE_NAME"));
System.out.printf("\n키 : %s \t값 : %s", "PORTAL_SERVICE_CD", ps.Pars(ApiRtn, "PORTAL_SERVICE_CD"));
System.out.printf("\n키 : %s \t값 : %s", "PORTAL_SERVICE_NAME", ps.Pars(ApiRtn, "PORTAL_SERVICE_NAME"));
System.out.printf("\n키 : %s \t값 : %s", "QUANTITY", ps.Pars(ApiRtn, "QUANTITY"));
System.out.printf("\n키 : %s \t값 : %s", "SYMBOL", ps.Pars(ApiRtn, "SYMBOL"));
System.out.printf("\n키 : %s \t값 : %s", "SERVICE_TYPE", ps.Pars(ApiRtn, "SERVICE_TYPE"));
} else {
System.out.printf("\nReturn : %s", ps.ReturnOK(ApiRtn) );
}
}
}
|