loadrunner读取xml例子

前端之家收集整理的这篇文章主要介绍了loadrunner读取xml例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
文件名test.xml,保存在D盘根目录下,内容为:
<employee>
<id>123</id>
<name>tom</name>
<add>beijing</add>
<age>20</age>
</employee>

loadrunner中的代码如下:

long fp;

int total = 0;
int readbufsize = 0;
int count = 0;
int i = 0;
int NumOfReadbuf_1 = 0;
int NumOfReadbuf_2 = 0;
int NumOfReadbuf_3 = 0;
int NumOfReadbuf_4 = 0;

char *readbuf;
char *file = "D:\\test.xml";

//读取XML文件
if ( (fp = fopen(file,"r")) == NULL)
{
lr_message("open logfile Failed!\n");
return -1;
}

//申请 readbuf

readbuf = (char*) calloc(50,sizeof(char));
if (readbuf == NULL)
{
lr_message("Insufficient memory available");
return -1;
}

//循环读取文件
while(!feof(fp))
{
// 扩展缓冲区
if( readbufsize - total <= 10 )
{
readbufsize += 50;
readbuf = (char*) realloc(readbuf,readbufsize);
}

count = fread(readbuf + total,sizeof(char),10,fp);

total += count;
}

//关闭文件句柄
fclose(fp);

//修改readbuf内存值
readbuf = (char*) realloc(readbuf,readbufsize+1);
*(readbuf+total) = '\0';

//保存参数
lr_save_string(readbuf,"readbuf_param");

//读取记录
NumOfReadbuf_1 = lr_xml_get_values("XML={readbuf_param}",
"ValueParam=id",
"Query=/employee/id",
"SelectAll=yes",
LAST);

NumOfReadbuf_2 = lr_xml_get_values("XML={readbuf_param}",
"ValueParam=name",
"Query=/employee/name",
LAST);

NumOfReadbuf_3 = lr_xml_get_values("XML={readbuf_param}",
"ValueParam=add",
"Query=/employee/add",
LAST);

NumOfReadbuf_4 = lr_xml_get_values("XML={readbuf_param}","ValueParam=age","Query=/employee/age","SelectAll=yes",LAST); //输出结果 lr_log_message("%s",lr_eval_string("{id_1}")); lr_log_message("%s",lr_eval_string("{name_1}")); lr_log_message("%s",lr_eval_string("{add_1}")); lr_log_message("%s",lr_eval_string("{age_1}"));

猜你在找的XML相关文章