Search code examples
regexxsltxpathjmeterbeanshell

how to save a xml list from response to a csv file in JMeter


one of my tests uses a Loop Controller, CSV Data Set Config and an If Controller to iterate through a csv list to do one request with several parameters defined in an csv file.

I want to change this testcase to use a list of parameters that i get from an GET response in an xml format. for example using this xml list: http://www.w3schools.com/xml/cd_catalog.xml now i want to iterate through all < TITLE > values.

i tried to save the response with the 'Save Responses to a file' Listener and then use a BeanShell Listener to read the file and transform it to a csv list which contains only the < TITLE > values. but i'm not sure how to to this transformation part in the BeanShell Listener.

import org.apache.jmeter.services.FileServer; 
import org.apache.commons.io.FileUtils;

File xmlFile = new File(FileServer.getFileServer().getBaseDir()+"/resources/data/csv/response1.xml", "UTF-8");
String fileData = FileUtils.readFileToString(xmlFile);
fileData = fileData.replaceAll("<?xml version="1.0" encoding="UTF-8"?>", "");
fileData = fileData.replaceAll("<CATALOG>", "");
//...

FileUtils.writeStringToFile(xmlFile, fileData);

/*
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"/resources/data/csv/parameterlist.csv", true); 
p = new PrintStream(f); 
p.println(fileData);
p.close();
f.close();
*/

there must be some solution using regex, xpath or XSL transformation on all < TITLE > elements or is there an easier way that i didn't thought of?


Solution

  • found a different and easier method that works for my purpose:

    • Get Request to get the xml list.
      • add XPath Extractor
      • Reference Name: 'titles'
      • XPAth query: '//title'
    • ForEach Controller
      • Input variable prefix: 'titles'
      • Output variable name: 'title'