LR对XML格式的字符串的提取方法

 

LRXML格式的字符串的提取方法,即获得子串的方法

 

Char *szResponse 的内容如下

<?xml version="1.0" encoding="GBK"?><CMBSDKPGK><INFO><DATTYP>2</DATTYP><ERRMSG>4262: 出现错误</ERRMSG><FUNNAM>AgentRequest</FUNNAM><LGNNAM>XNCS1</LGNNAM><RETCOD>-9</RETCOD></INFO></CMBSDKPGK>

 

需要提取<RETCOD>字段 及<ERRMSG>所包含的内容。其中,RETCOD为0,或者负数,0代表正常,负数则对应相应的错误。当RETCOD不为0时,<ERRMSG>则给出具体的错误信息。

 

由于LR所支持的ANSIC中并没有substr函数,所以不能直接对szResponse 进行子串操作。

以下是几种处理方法:

 

1  查阅LR的帮助中关于C函数的内容。利用以下两函数可实现

    

Returns the first occurrence of one string in another.

 

char *strstr ( const char *string1, const char *string2 );

 

string1 The string that is searched.

string2 The string that is searched for in the first string.

strstr returns the first occurrence of one string in another.

 

 

Copies the first n characters of one string to another.

 

char *strncpy ( char *dest, const char *source, size_t n );

 

dest  The destination string to which n characters are copied.

source  The source string from which n characters are copied.

n  The number of characters copied.

strncpy copies the first n characters of one string to another.

 

首先利用strstr 分别查找<RETCOD></RETCOD>szResponse 中的地址,从而计算出它们所包含内容的首地址与长度;然后利用strncpy 获得所字段内容。

 

iFirstRetCode =  strstr(szResponse,"<RETCOD>");

 

iSecondRetCode = strstr(szResponse,"</RETCOD>");

 

strncpy(szRetCode , (char*)(iFirstRetCode + iSecondRetCode), iSecondRetCode -iFirstRetCode - strlen("<RETCOD>"));

 

其中,szRetCode 为所求。

 

2 与第一种方法类似,前面依然采用strstr 但后面则利用memcpy

 

iFirstRetCode =  strstr(szResponse,"<RETCOD>");

iSecondRetCode = strstr(szResponse,"</RETCOD>");

strncpy(szRetCode,(char *)(iFirstRetCode + strlen("<RETCOD>"), iSecondRetCode -iFirstRetCode - strlen("<RETCOD>") )                                                  

 

3  每三种方法则不利用C提供的字符串函数,而是利用LR关于XML处理函数lr_xml_get_values

 

     lr_save_string(szResponse , "XML_Input_Param"); // Save szResponse as

     lr_xml_get_values("XML={XML_Input_Param}",

 

          "ValueParam=OutputParam",

 

          "Query=/CMBSDKPGK/RETCOD",        //这里指明所求内容所在XML的结构层次

 

          LAST);

 

     lr_output_message(lr_eval_string("Query result = {OutputParam}"));

 

个人推荐第三种方法

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值