如何在JMeter中读取XML文件?

我试过了:

//${__FileToString(C:\\QC\\qa\\Testlink\\Jmeter\\Expected\\test.xml,ASCII,${xmlFile})};

发现错误消息: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``// <feed xmlns="http://www.w3.org/2005/At . . . '' Encountered "<" at line 2, column 1.

另外,我尝试使用${__StringFromFile}并得到相同的错误消息,甚至使用beanshell脚本:

 import org.apache.jmeter.services.FileServer; //Open the file FileInputStream fstream = new FileInputStream("C://QC//qa//Testlink//Jmeter//Expected//test.xml"); //Get the object of DataInputStream DataInputStream instream = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(instream)); 

试试以下内容:

  1. 将Beanshell Sampler添加到您的测试计划中
  2. 将以下代码放入采样器的“脚本”区域:

     import org.apache.commons.io.FileUtils; try { String content = FileUtils.readFileToString(new File("C:/QC/qa/Testlink/Jmeter/Expected/test.xml")); vars.put("content", content); } catch (Throwable ex) { log.info("Failed to read \"test.xml\" file", ex); throw ex; } 
  3. 将Debug Sampler和View Results Tree监听器添加到测试计划中

  4. 运行测试
  5. 确保Beanshell Sampler为绿色且设置了${content}变量。 如果不是 – 查看jmeter.log文件并搜索Failed to read "test.xml" file 。 如果此行下面的exception堆栈跟踪告诉您什么 – 在此处发布。

有关在JMeter测试中使用Beanshell的更多信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件指南。