Q:

Jmeter: Replace data in current sampler from JSR223 pre processor

I need to sign my request xml and add a token to it before hitting the application. For that I am using a JSR223 preprocessor which gets the data from current sampler, makes the necessary changes and puts the data back to sampler. This approach works fine with a single thread group. When multiple thread groups are used data between the samplers(in different thread groups) gets interchanged and it results in incorrect requests for the thread group. Below is the code in my preprocessor:
import jmeter_plugin.*;
String body = sampler.getXmlData();
log.info(body);
Utils utils=new Utils();
String request=utils.getResponse( body,"url/to/fetch/token");
log.info(request);
sampler.setXmlData(request);

Tried by having a single preprocessors for the entire test plan and also separate pre processors for each thread group. Both approaches did not work. 

A:

Your approach should work fine no matter how many Thread Groups do you have. Pre-Processors are executed before each sampler in its scope and should modify only the current sampler. 
Quick checklist:

Make sure you're using Groovy as JSR223 PreProcessor language 
Make sure you have Cache compiled script if available box ticked

Remove these log.info lines as they create unnecessary Disk IO overhead
Make sure your Utils class don't use methods which are not Thread Safe
Looking into sampler.getXmlData() it appears you're using JMeter 3.1 or below as SOAP/XML-RPC Request has been removed in JMeter 3.2 and users are encouraged to use HTTP Request sampler instead. 
If your "sign" algorithms is not very exotic if might be easier to use WS Security for SOAP JMeter Plugin which can be installed using JMeter Plugins Manager 

